blob: 0bb88e8a99ae8d3bb59c24d223d741f9bc78061b [file] [log] [blame]
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001/*
2 * Copyright (c) 2012, Code Aurora Forum. All rights reserved.
3 * Author: Brian Swetland <swetland@google.com>
4 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15#include <linux/fs.h>
16#include <linux/mutex.h>
17#include <linux/wait.h>
18#include <linux/miscdevice.h>
19#include <linux/uaccess.h>
20#include <linux/sched.h>
21#include <linux/dma-mapping.h>
22#include <linux/miscdevice.h>
23#include <linux/delay.h>
24#include <linux/spinlock.h>
25#include <linux/slab.h>
26#include <linux/msm_audio.h>
27#include <linux/android_pmem.h>
28#include <linux/memory_alloc.h>
29#include <linux/debugfs.h>
30#include <linux/time.h>
31#include <linux/atomic.h>
32
33#include <asm/ioctls.h>
34
35#include <mach/memory.h>
36#include <mach/debug_mm.h>
37#include <mach/peripheral-loader.h>
38#include <mach/qdsp6v2/audio_acdb.h>
39#include <mach/qdsp6v2/rtac.h>
40#include <mach/msm_subsystem_map.h>
41
42#include <sound/apr_audio-v2.h>
43#include <sound/q6asm-v2.h>
44
45#define TRUE 0x01
46#define FALSE 0x00
47#define READDONE_IDX_STATUS 0
48#define READDONE_IDX_BUFADD_LSW 1
49#define READDONE_IDX_BUFADD_MSW 2
50#define READDONE_IDX_MEMMAP_HDL 3
51#define READDONE_IDX_SIZE 4
52#define READDONE_IDX_OFFSET 5
53#define READDONE_IDX_LSW_TS 6
54#define READDONE_IDX_MSW_TS 7
55#define READDONE_IDX_FLAGS 8
56#define READDONE_IDX_NUMFRAMES 9
57#define READDONE_IDX_SEQ_ID 10
58
59/* TODO, combine them together */
60static DEFINE_MUTEX(session_lock);
61struct asm_mmap {
62 atomic_t ref_cnt;
63 void *apr;
64};
65
66static struct asm_mmap this_mmap;
67/* session id: 0 reserved */
68static struct audio_client *session[SESSION_MAX+1];
69
70struct asm_buffer_node {
71 struct list_head list;
72 uint32_t buf_addr_lsw;
73 uint32_t mmap_hdl;
74};
75static int32_t q6asm_mmapcallback(struct apr_client_data *data, void *priv);
76static int32_t q6asm_callback(struct apr_client_data *data, void *priv);
77static void q6asm_add_hdr(struct audio_client *ac, struct apr_hdr *hdr,
78 uint32_t pkt_size, uint32_t cmd_flg);
79static void q6asm_add_hdr_async(struct audio_client *ac, struct apr_hdr *hdr,
80 uint32_t pkt_size, uint32_t cmd_flg);
81static int q6asm_memory_map_regions(struct audio_client *ac, int dir,
82 uint32_t bufsz, uint32_t bufcnt);
83static int q6asm_memory_unmap_regions(struct audio_client *ac, int dir,
84 uint32_t bufsz, uint32_t bufcnt);
85static void q6asm_reset_buf_state(struct audio_client *ac);
86
Harmandeep Singheaf59b42012-06-05 21:46:02 -070087static int q6asm_map_channels(u8 *channel_mapping, uint32_t channels);
88
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -070089
90#ifdef CONFIG_DEBUG_FS
91#define OUT_BUFFER_SIZE 56
92#define IN_BUFFER_SIZE 24
93
94static struct timeval out_cold_tv;
95static struct timeval out_warm_tv;
96static struct timeval out_cont_tv;
97static struct timeval in_cont_tv;
98static long out_enable_flag;
99static long in_enable_flag;
100static struct dentry *out_dentry;
101static struct dentry *in_dentry;
102static int in_cont_index;
103/*This var is used to keep track of first write done for cold output latency */
104static int out_cold_index;
105static char *out_buffer;
106static char *in_buffer;
107static int audio_output_latency_dbgfs_open(struct inode *inode,
108 struct file *file)
109{
110 file->private_data = inode->i_private;
111 return 0;
112}
113static ssize_t audio_output_latency_dbgfs_read(struct file *file,
114 char __user *buf, size_t count, loff_t *ppos)
115{
116 snprintf(out_buffer, OUT_BUFFER_SIZE, "%ld,%ld,%ld,%ld,%ld,%ld,",\
117 out_cold_tv.tv_sec, out_cold_tv.tv_usec, out_warm_tv.tv_sec,\
118 out_warm_tv.tv_usec, out_cont_tv.tv_sec, out_cont_tv.tv_usec);
119 return simple_read_from_buffer(buf, OUT_BUFFER_SIZE, ppos,
120 out_buffer, OUT_BUFFER_SIZE);
121}
122static ssize_t audio_output_latency_dbgfs_write(struct file *file,
123 const char __user *buf, size_t count, loff_t *ppos)
124{
125 char *temp;
126
127 if (count > 2*sizeof(char))
128 return -EINVAL;
129 else
130 temp = kmalloc(2*sizeof(char), GFP_KERNEL);
131
132 out_cold_index = 0;
133
134 if (temp) {
135 if (copy_from_user(temp, buf, 2*sizeof(char))) {
136 kfree(temp);
137 return -EFAULT;
138 }
139 if (!strict_strtol(temp, 10, &out_enable_flag)) {
140 kfree(temp);
141 return count;
142 }
143 kfree(temp);
144 }
145 return -EINVAL;
146}
147static const struct file_operations audio_output_latency_debug_fops = {
148 .open = audio_output_latency_dbgfs_open,
149 .read = audio_output_latency_dbgfs_read,
150 .write = audio_output_latency_dbgfs_write
151};
152static int audio_input_latency_dbgfs_open(struct inode *inode,
153 struct file *file)
154{
155 file->private_data = inode->i_private;
156 return 0;
157}
158static ssize_t audio_input_latency_dbgfs_read(struct file *file,
159 char __user *buf, size_t count, loff_t *ppos)
160{
161 snprintf(in_buffer, IN_BUFFER_SIZE, "%ld,%ld,",\
162 in_cont_tv.tv_sec, in_cont_tv.tv_usec);
163 return simple_read_from_buffer(buf, IN_BUFFER_SIZE, ppos,
164 in_buffer, IN_BUFFER_SIZE);
165}
166static ssize_t audio_input_latency_dbgfs_write(struct file *file,
167 const char __user *buf, size_t count, loff_t *ppos)
168{
169 char *temp;
170
171 if (count > 2*sizeof(char))
172 return -EINVAL;
173 else
174 temp = kmalloc(2*sizeof(char), GFP_KERNEL);
175 if (temp) {
176 if (copy_from_user(temp, buf, 2*sizeof(char))) {
177 kfree(temp);
178 return -EFAULT;
179 }
180 if (!strict_strtol(temp, 10, &in_enable_flag)) {
181 kfree(temp);
182 return count;
183 }
184 kfree(temp);
185 }
186 return -EINVAL;
187}
188static const struct file_operations audio_input_latency_debug_fops = {
189 .open = audio_input_latency_dbgfs_open,
190 .read = audio_input_latency_dbgfs_read,
191 .write = audio_input_latency_dbgfs_write
192};
193
194static void config_debug_fs_write_cb(void)
195{
196 if (out_enable_flag) {
197 /* For first Write done log the time and reset
198 out_cold_index*/
199 if (out_cold_index != 1) {
200 do_gettimeofday(&out_cold_tv);
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700201 pr_debug("COLD: apr_send_pkt at %ld sec %ld microsec\n",
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700202 out_cold_tv.tv_sec,\
203 out_cold_tv.tv_usec);
204 out_cold_index = 1;
205 }
206 pr_debug("out_enable_flag %ld",\
207 out_enable_flag);
208 }
209}
210static void config_debug_fs_read_cb(void)
211{
212 if (in_enable_flag) {
213 /* when in_cont_index == 7, DSP would be
214 * writing into the 8th 512 byte buffer and this
215 * timestamp is tapped here.Once done it then writes
216 * to 9th 512 byte buffer.These two buffers(8th, 9th)
217 * reach the test application in 5th iteration and that
218 * timestamp is tapped at user level. The difference
219 * of these two timestamps gives us the time between
220 * the time at which dsp started filling the sample
221 * required and when it reached the test application.
222 * Hence continuous input latency
223 */
224 if (in_cont_index == 7) {
225 do_gettimeofday(&in_cont_tv);
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700226 pr_err("In_CONT:previous read buffer done at %ld sec %ld microsec\n",
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700227 in_cont_tv.tv_sec, in_cont_tv.tv_usec);
228 }
229 in_cont_index++;
230 }
231}
232
233static void config_debug_fs_reset_index(void)
234{
235 in_cont_index = 0;
236}
237
238static void config_debug_fs_run(void)
239{
240 if (out_enable_flag) {
241 do_gettimeofday(&out_cold_tv);
242 pr_debug("COLD: apr_send_pkt at %ld sec %ld microsec\n",\
243 out_cold_tv.tv_sec, out_cold_tv.tv_usec);
244 }
245}
246
247static void config_debug_fs_write(struct audio_buffer *ab)
248{
249 if (out_enable_flag) {
250 char zero_pattern[2] = {0x00, 0x00};
251 /* If First two byte is non zero and last two byte
252 is zero then it is warm output pattern */
253 if ((strncmp(((char *)ab->data), zero_pattern, 2)) &&
254 (!strncmp(((char *)ab->data + 2), zero_pattern, 2))) {
255 do_gettimeofday(&out_warm_tv);
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700256 pr_debug("WARM:apr_send_pkt at %ld sec %ld microsec\n",
257 out_warm_tv.tv_sec,\
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700258 out_warm_tv.tv_usec);
259 pr_debug("Warm Pattern Matched");
260 }
261 /* If First two byte is zero and last two byte is
262 non zero then it is cont ouput pattern */
263 else if ((!strncmp(((char *)ab->data), zero_pattern, 2))
264 && (strncmp(((char *)ab->data + 2), zero_pattern, 2))) {
265 do_gettimeofday(&out_cont_tv);
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700266 pr_debug("CONT:apr_send_pkt at %ld sec %ld microsec\n",
267 out_cont_tv.tv_sec,\
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700268 out_cont_tv.tv_usec);
269 pr_debug("Cont Pattern Matched");
270 }
271 }
272}
273static void config_debug_fs_init(void)
274{
275 out_buffer = kmalloc(OUT_BUFFER_SIZE, GFP_KERNEL);
276 out_dentry = debugfs_create_file("audio_out_latency_measurement_node",\
277 S_IFREG | S_IRUGO | S_IWUGO,\
278 NULL, NULL, &audio_output_latency_debug_fops);
279 if (IS_ERR(out_dentry))
280 pr_err("debugfs_create_file failed\n");
281 in_buffer = kmalloc(IN_BUFFER_SIZE, GFP_KERNEL);
282 in_dentry = debugfs_create_file("audio_in_latency_measurement_node",\
283 S_IFREG | S_IRUGO | S_IWUGO,\
284 NULL, NULL, &audio_input_latency_debug_fops);
285 if (IS_ERR(in_dentry))
286 pr_err("debugfs_create_file failed\n");
287}
288#else
289static void config_debug_fs_write(struct audio_buffer *ab)
290{
291 return;
292}
293static void config_debug_fs_run(void)
294{
295 return;
296}
297static void config_debug_fs_reset_index(void)
298{
299 return;
300}
301static void config_debug_fs_read_cb(void)
302{
303 return;
304}
305static void config_debug_fs_write_cb(void)
306{
307 return;
308}
309static void config_debug_fs_init(void)
310{
311 return;
312}
313#endif
314
315
316static int q6asm_session_alloc(struct audio_client *ac)
317{
318 int n;
319 mutex_lock(&session_lock);
320 for (n = 1; n <= SESSION_MAX; n++) {
321 if (!session[n]) {
322 session[n] = ac;
323 mutex_unlock(&session_lock);
324 return n;
325 }
326 }
327 mutex_unlock(&session_lock);
328 return -ENOMEM;
329}
330
331static void q6asm_session_free(struct audio_client *ac)
332{
333 pr_debug("%s: sessionid[%d]\n", __func__, ac->session);
334 rtac_remove_popp_from_adm_devices(ac->session);
335 mutex_lock(&session_lock);
336 session[ac->session] = 0;
337 mutex_unlock(&session_lock);
338 ac->session = 0;
339 return;
340}
341
342int q6asm_audio_client_buf_free(unsigned int dir,
343 struct audio_client *ac)
344{
345 struct audio_port_data *port;
346 int cnt = 0;
347 int rc = 0;
348 pr_debug("%s: Session id %d\n", __func__, ac->session);
349 mutex_lock(&ac->cmd_lock);
350 if (ac->io_mode == SYNC_IO_MODE) {
351 port = &ac->port[dir];
352 if (!port->buf) {
353 mutex_unlock(&ac->cmd_lock);
354 return 0;
355 }
356 cnt = port->max_buf_cnt - 1;
357
358 if (cnt >= 0) {
359 rc = q6asm_memory_unmap_regions(ac, dir,
360 port->buf[0].size,
361 port->max_buf_cnt);
362 if (rc < 0)
363 pr_err("%s CMD Memory_unmap_regions failed\n",
364 __func__);
365 }
366
367 while (cnt >= 0) {
368 if (port->buf[cnt].data) {
369 ion_unmap_kernel(port->buf[cnt].client,
370 port->buf[cnt].handle);
371 ion_free(port->buf[cnt].client,
372 port->buf[cnt].handle);
373 ion_client_destroy(port->buf[cnt].client);
374 port->buf[cnt].data = NULL;
375 port->buf[cnt].phys = 0;
376 --(port->max_buf_cnt);
377 }
378 --cnt;
379 }
380 kfree(port->buf);
381 port->buf = NULL;
382 }
383 mutex_unlock(&ac->cmd_lock);
384 return 0;
385}
386
387int q6asm_audio_client_buf_free_contiguous(unsigned int dir,
388 struct audio_client *ac)
389{
390 struct audio_port_data *port;
391 int cnt = 0;
392 int rc = 0;
393 pr_debug("%s: Session id %d\n", __func__, ac->session);
394 mutex_lock(&ac->cmd_lock);
395 port = &ac->port[dir];
396 if (!port->buf) {
397 mutex_unlock(&ac->cmd_lock);
398 return 0;
399 }
400 cnt = port->max_buf_cnt - 1;
401
402 if (cnt >= 0) {
403 rc = q6asm_memory_unmap(ac, port->buf[0].phys, dir);
404 if (rc < 0)
405 pr_err("%s CMD Memory_unmap_regions failed\n",
406 __func__);
407 }
408
409 if (port->buf[0].data) {
410 ion_unmap_kernel(port->buf[0].client, port->buf[0].handle);
411 ion_free(port->buf[0].client, port->buf[0].handle);
412 ion_client_destroy(port->buf[0].client);
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700413 pr_debug("%s:data[%p]phys[%p][%p] , client[%p] handle[%p]\n",
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700414 __func__,
415 (void *)port->buf[0].data,
416 (void *)port->buf[0].phys,
417 (void *)&port->buf[0].phys,
418 (void *)port->buf[0].client,
419 (void *)port->buf[0].handle);
420 }
421
422 while (cnt >= 0) {
423 port->buf[cnt].data = NULL;
424 port->buf[cnt].phys = 0;
425 cnt--;
426 }
427 port->max_buf_cnt = 0;
428 kfree(port->buf);
429 port->buf = NULL;
430 mutex_unlock(&ac->cmd_lock);
431 return 0;
432}
433
434int q6asm_mmap_apr_dereg(void)
435{
436 if (atomic_read(&this_mmap.ref_cnt) <= 0) {
437 pr_err("%s: APR Common Port Already Closed\n", __func__);
438 goto done;
439 }
440 atomic_dec(&this_mmap.ref_cnt);
441 if (atomic_read(&this_mmap.ref_cnt) == 0) {
442 apr_deregister(this_mmap.apr);
443 pr_debug("%s:APR De-Register common port\n", __func__);
444 }
445done:
446 return 0;
447}
448
449
450void q6asm_audio_client_free(struct audio_client *ac)
451{
452 int loopcnt;
453 struct audio_port_data *port;
454 if (!ac || !ac->session)
455 return;
456 pr_debug("%s: Session id %d\n", __func__, ac->session);
457 if (ac->io_mode == SYNC_IO_MODE) {
458 for (loopcnt = 0; loopcnt <= OUT; loopcnt++) {
459 port = &ac->port[loopcnt];
460 if (!port->buf)
461 continue;
462 pr_debug("%s:loopcnt = %d\n", __func__, loopcnt);
463 q6asm_audio_client_buf_free(loopcnt, ac);
464 }
465 }
466
467 apr_deregister(ac->apr);
468 ac->mmap_apr = NULL;
469 q6asm_session_free(ac);
470 q6asm_mmap_apr_dereg();
471
472 pr_debug("%s: APR De-Register\n", __func__);
473
474/*done:*/
475 kfree(ac);
476 return;
477}
478
479int q6asm_set_io_mode(struct audio_client *ac, uint32_t mode)
480{
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700481 ac->io_mode &= 0xFF00;
482 pr_debug("%s ac->mode after anding with FF00:0x[%x],\n",
483 __func__, ac->io_mode);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700484 if (ac == NULL) {
485 pr_err("%s APR handle NULL\n", __func__);
486 return -EINVAL;
487 }
488 if ((mode == ASYNC_IO_MODE) || (mode == SYNC_IO_MODE)) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700489 ac->io_mode |= mode;
490 pr_debug("%s:Set Mode to 0x[%x]\n", __func__, ac->io_mode);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700491 return 0;
492 } else {
493 pr_err("%s:Not an valid IO Mode:%d\n", __func__, ac->io_mode);
494 return -EINVAL;
495 }
496}
497
498void *q6asm_mmap_apr_reg(void)
499{
500 if (atomic_read(&this_mmap.ref_cnt) == 0) {
501 this_mmap.apr = apr_register("ADSP", "ASM", \
502 (apr_fn)q6asm_mmapcallback,\
503 0x0FFFFFFFF, &this_mmap);
504 if (this_mmap.apr == NULL) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700505 pr_debug("%s Unable to register APR ASM common port\n",
506 __func__);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700507 goto fail;
508 }
509 }
510 atomic_inc(&this_mmap.ref_cnt);
511 return this_mmap.apr;
512fail:
513 return NULL;
514}
515
516struct audio_client *q6asm_audio_client_alloc(app_cb cb, void *priv)
517{
518 struct audio_client *ac;
519 int n;
520 int lcnt = 0;
521
522 ac = kzalloc(sizeof(struct audio_client), GFP_KERNEL);
523 if (!ac)
524 return NULL;
525 n = q6asm_session_alloc(ac);
526 if (n <= 0)
527 goto fail_session;
528 ac->session = n;
529 ac->cb = cb;
530 ac->priv = priv;
531 ac->io_mode = SYNC_IO_MODE;
532 ac->apr = apr_register("ADSP", "ASM", \
533 (apr_fn)q6asm_callback,\
534 ((ac->session) << 8 | 0x0001),\
535 ac);
536
537 if (ac->apr == NULL) {
538 pr_err("%s Registration with APR failed\n", __func__);
539 goto fail;
540 }
541 rtac_set_asm_handle(n, ac->apr);
542
543 pr_debug("%s Registering the common port with APR\n", __func__);
544 ac->mmap_apr = q6asm_mmap_apr_reg();
545 if (ac->mmap_apr == NULL)
546 goto fail;
547
548 init_waitqueue_head(&ac->cmd_wait);
549 INIT_LIST_HEAD(&ac->port[0].mem_map_handle);
550 INIT_LIST_HEAD(&ac->port[1].mem_map_handle);
551 pr_debug("%s: mem_map_handle list init'ed\n", __func__);
552 mutex_init(&ac->cmd_lock);
553 for (lcnt = 0; lcnt <= OUT; lcnt++) {
554 mutex_init(&ac->port[lcnt].lock);
555 spin_lock_init(&ac->port[lcnt].dsp_lock);
556 }
557 atomic_set(&ac->cmd_state, 0);
558
559 pr_debug("%s: session[%d]\n", __func__, ac->session);
560
561 return ac;
562fail:
563 q6asm_audio_client_free(ac);
564 return NULL;
565fail_session:
566 kfree(ac);
567 return NULL;
568}
569
570struct audio_client *q6asm_get_audio_client(int session_id)
571{
572 if ((session_id <= 0) || (session_id > SESSION_MAX)) {
573 pr_err("%s: invalid session: %d\n", __func__, session_id);
574 goto err;
575 }
576
577 if (!session[session_id]) {
578 pr_err("%s: session not active: %d\n", __func__, session_id);
579 goto err;
580 }
581
582 return session[session_id];
583err:
584 return NULL;
585}
586
587int q6asm_audio_client_buf_alloc(unsigned int dir,
588 struct audio_client *ac,
589 unsigned int bufsz,
590 unsigned int bufcnt)
591{
592 int cnt = 0;
593 int rc = 0;
594 struct audio_buffer *buf;
595 int len;
596
597 if (!(ac) || ((dir != IN) && (dir != OUT)))
598 return -EINVAL;
599
600 pr_debug("%s: session[%d]bufsz[%d]bufcnt[%d]\n", __func__, ac->session,
601 bufsz, bufcnt);
602
603 if (ac->session <= 0 || ac->session > 8)
604 goto fail;
605
606 if (ac->io_mode == SYNC_IO_MODE) {
607 if (ac->port[dir].buf) {
608 pr_debug("%s: buffer already allocated\n", __func__);
609 return 0;
610 }
611 mutex_lock(&ac->cmd_lock);
612 buf = kzalloc(((sizeof(struct audio_buffer))*bufcnt),
613 GFP_KERNEL);
614
615 if (!buf) {
616 mutex_unlock(&ac->cmd_lock);
617 goto fail;
618 }
619
620 ac->port[dir].buf = buf;
621
622 while (cnt < bufcnt) {
623 if (bufsz > 0) {
624 if (!buf[cnt].data) {
625 buf[cnt].client = msm_ion_client_create
626 (UINT_MAX, "audio_client");
627 if (IS_ERR_OR_NULL((void *)
628 buf[cnt].client)) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700629 pr_err("%s: ION create client for AUDIO failed\n",
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700630 __func__);
631 goto fail;
632 }
633 buf[cnt].handle = ion_alloc
634 (buf[cnt].client, bufsz, SZ_4K,
635 (0x1 << ION_AUDIO_HEAP_ID));
636 if (IS_ERR_OR_NULL((void *)
637 buf[cnt].handle)) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700638 pr_err("%s: ION memory allocation for AUDIO failed\n",
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700639 __func__);
640 goto fail;
641 }
642
643 rc = ion_phys(buf[cnt].client,
644 buf[cnt].handle,
645 (ion_phys_addr_t *)
646 &buf[cnt].phys,
647 (size_t *)&len);
648 if (rc) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700649 pr_err("%s: ION Get Physical for AUDIO failed, rc = %d\n",
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700650 __func__, rc);
651 goto fail;
652 }
653
654 buf[cnt].data = ion_map_kernel
655 (buf[cnt].client, buf[cnt].handle,
656 0);
657 if (IS_ERR_OR_NULL((void *)
658 buf[cnt].data)) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700659 pr_err("%s: ION memory mapping for AUDIO failed\n",
660 __func__);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700661 goto fail;
662 }
663 memset((void *)buf[cnt].data, 0, bufsz);
664 buf[cnt].used = 1;
665 buf[cnt].size = bufsz;
666 buf[cnt].actual_size = bufsz;
667 pr_debug("%s data[%p]phys[%p][%p]\n",
668 __func__,
669 (void *)buf[cnt].data,
670 (void *)buf[cnt].phys,
671 (void *)&buf[cnt].phys);
672 cnt++;
673 }
674 }
675 }
676 ac->port[dir].max_buf_cnt = cnt;
677
678 mutex_unlock(&ac->cmd_lock);
679 rc = q6asm_memory_map_regions(ac, dir, bufsz, cnt);
680 if (rc < 0) {
681 pr_err("%s:CMD Memory_map_regions failed\n", __func__);
682 goto fail;
683 }
684 }
685 return 0;
686fail:
687 q6asm_audio_client_buf_free(dir, ac);
688 return -EINVAL;
689}
690
691int q6asm_audio_client_buf_alloc_contiguous(unsigned int dir,
692 struct audio_client *ac,
693 unsigned int bufsz,
694 unsigned int bufcnt)
695{
696 int cnt = 0;
697 int rc = 0;
698 struct audio_buffer *buf;
699 int len;
700
701 if (!(ac) || ((dir != IN) && (dir != OUT)))
702 return -EINVAL;
703
704 pr_debug("%s: session[%d]bufsz[%d]bufcnt[%d]\n",
705 __func__, ac->session,
706 bufsz, bufcnt);
707
708 if (ac->session <= 0 || ac->session > 8)
709 goto fail;
710
711 if (ac->port[dir].buf) {
712 pr_debug("%s: buffer already allocated\n", __func__);
713 return 0;
714 }
715 mutex_lock(&ac->cmd_lock);
716 buf = kzalloc(((sizeof(struct audio_buffer))*bufcnt),
717 GFP_KERNEL);
718
719 if (!buf) {
720 mutex_unlock(&ac->cmd_lock);
721 goto fail;
722 }
723
724 ac->port[dir].buf = buf;
725
726 buf[0].client = msm_ion_client_create(UINT_MAX, "audio_client");
727 if (IS_ERR_OR_NULL((void *)buf[0].client)) {
728 pr_err("%s: ION create client for AUDIO failed\n", __func__);
729 goto fail;
730 }
731 buf[0].handle = ion_alloc(buf[0].client, bufsz * bufcnt, SZ_4K,
732 (0x1 << ION_AUDIO_HEAP_ID));
733 if (IS_ERR_OR_NULL((void *) buf[0].handle)) {
734 pr_err("%s: ION memory allocation for AUDIO failed\n",
735 __func__);
736 goto fail;
737 }
738
739 rc = ion_phys(buf[0].client, buf[0].handle,
740 (ion_phys_addr_t *)&buf[0].phys, (size_t *)&len);
741 if (rc) {
742 pr_err("%s: ION Get Physical for AUDIO failed, rc = %d\n",
743 __func__, rc);
744 goto fail;
745 }
746
747 buf[0].data = ion_map_kernel(buf[0].client, buf[0].handle, 0);
748 if (IS_ERR_OR_NULL((void *) buf[0].data)) {
749 pr_err("%s: ION memory mapping for AUDIO failed\n", __func__);
750 goto fail;
751 }
752 memset((void *)buf[0].data, 0, (bufsz * bufcnt));
753 if (!buf[0].data) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700754 pr_err("%s:invalid vaddr, iomap failed\n", __func__);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700755 mutex_unlock(&ac->cmd_lock);
756 goto fail;
757 }
758
759 buf[0].used = dir ^ 1;
760 buf[0].size = bufsz;
761 buf[0].actual_size = bufsz;
762 cnt = 1;
763 while (cnt < bufcnt) {
764 if (bufsz > 0) {
765 buf[cnt].data = buf[0].data + (cnt * bufsz);
766 buf[cnt].phys = buf[0].phys + (cnt * bufsz);
767 if (!buf[cnt].data) {
768 pr_err("%s Buf alloc failed\n",
769 __func__);
770 mutex_unlock(&ac->cmd_lock);
771 goto fail;
772 }
773 buf[cnt].used = dir ^ 1;
774 buf[cnt].size = bufsz;
775 buf[cnt].actual_size = bufsz;
776 pr_debug("%s data[%p]phys[%p][%p]\n", __func__,
777 (void *)buf[cnt].data,
778 (void *)buf[cnt].phys,
779 (void *)&buf[cnt].phys);
780 }
781 cnt++;
782 }
783 ac->port[dir].max_buf_cnt = cnt;
784 mutex_unlock(&ac->cmd_lock);
785 rc = q6asm_memory_map_regions(ac, dir, bufsz, cnt);
786 if (rc < 0) {
787 pr_err("%s:CMD Memory_map_regions failed\n", __func__);
788 goto fail;
789 }
790 return 0;
791fail:
792 q6asm_audio_client_buf_free_contiguous(dir, ac);
793 return -EINVAL;
794}
795
796static int32_t q6asm_mmapcallback(struct apr_client_data *data, void *priv)
797{
798 uint32_t sid = 0;
799 uint32_t dir = 0;
800 uint32_t *payload = data->payload;
801 unsigned long dsp_flags;
802
803 struct audio_client *ac = NULL;
804 struct audio_port_data *port;
805
806 if (!data) {
807 pr_err("%s: Invalid CB\n", __func__);
808 return 0;
809 }
810 if (data->opcode == RESET_EVENTS) {
811 pr_debug("%s: Reset event is received: %d %d apr[%p]\n",
812 __func__,
813 data->reset_event,
814 data->reset_proc,
815 this_mmap.apr);
816 apr_reset(this_mmap.apr);
817 atomic_set(&this_mmap.ref_cnt, 0);
818 this_mmap.apr = NULL;
819 return 0;
820 }
821 sid = (data->token >> 8) & 0x0F;
822 ac = q6asm_get_audio_client(sid);
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700823 pr_debug("%s:ptr0[0x%x]ptr1[0x%x]opcode[0x%x] token[0x%x]payload_s[%d] src[%d] dest[%d]sid[%d]dir[%d]\n",
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700824 __func__, payload[0], payload[1], data->opcode, data->token,
825 data->payload_size, data->src_port, data->dest_port, sid, dir);
826 pr_debug("%s:Payload = [0x%x] status[0x%x]\n",
827 __func__, payload[0], payload[1]);
828
829 if (data->opcode == APR_BASIC_RSP_RESULT) {
830 switch (payload[0]) {
831 case ASM_CMD_SHARED_MEM_MAP_REGIONS:
832 case ASM_CMD_SHARED_MEM_UNMAP_REGIONS:
833 if (atomic_read(&ac->cmd_state)) {
834 atomic_set(&ac->cmd_state, 0);
835 wake_up(&ac->cmd_wait);
836 }
837 pr_debug("%s:Payload = [0x%x] status[0x%x]\n",
838 __func__, payload[0], payload[1]);
839 break;
840 default:
841 pr_debug("%s:command[0x%x] not expecting rsp\n",
842 __func__, payload[0]);
843 break;
844 }
845 return 0;
846 }
847
848 dir = (data->token & 0x0F);
849 port = &ac->port[dir];
850
851 switch (data->opcode) {
852 case ASM_CMDRSP_SHARED_MEM_MAP_REGIONS:{
853 pr_debug("%s:PL#0[0x%x]PL#1 [0x%x] dir=%x s_id=%x\n",
854 __func__, payload[0], payload[1], dir, sid);
855 spin_lock_irqsave(&port->dsp_lock, dsp_flags);
856 if (atomic_read(&ac->cmd_state)) {
857 ac->port[dir].tmp_hdl = payload[0];
858 atomic_set(&ac->cmd_state, 0);
859 wake_up(&ac->cmd_wait);
860 }
861 spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
862 break;
863 }
864 case ASM_CMD_SHARED_MEM_UNMAP_REGIONS:{
865 pr_debug("%s:PL#0[0x%x]PL#1 [0x%x]\n",
866 __func__, payload[0], payload[1]);
867 spin_lock_irqsave(&port->dsp_lock, dsp_flags);
868 if (atomic_read(&ac->cmd_state)) {
869 atomic_set(&ac->cmd_state, 0);
870 wake_up(&ac->cmd_wait);
871 }
872 spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
873
874 break;
875 }
876 default:
877 pr_debug("%s:command[0x%x]success [0x%x]\n",
878 __func__, payload[0], payload[1]);
879 }
880 if (ac->cb)
881 ac->cb(data->opcode, data->token,
882 data->payload, ac->priv);
883 return 0;
884}
885
886
887static int32_t q6asm_callback(struct apr_client_data *data, void *priv)
888{
889 int i = 0;
890 struct audio_client *ac = (struct audio_client *)priv;
891 uint32_t token;
892 unsigned long dsp_flags;
893 uint32_t *payload;
894
895
896 if ((ac == NULL) || (data == NULL)) {
897 pr_err("ac or priv NULL\n");
898 return -EINVAL;
899 }
900 if (ac->session <= 0 || ac->session > 8) {
901 pr_err("%s:Session ID is invalid, session = %d\n", __func__,
902 ac->session);
903 return -EINVAL;
904 }
905
906 payload = data->payload;
907
908 if (data->opcode == RESET_EVENTS) {
909 pr_debug("q6asm_callback: Reset event is received: %d %d apr[%p]\n",
910 data->reset_event, data->reset_proc, ac->apr);
911 if (ac->cb)
912 ac->cb(data->opcode, data->token,
913 (uint32_t *)data->payload, ac->priv);
914 apr_reset(ac->apr);
915 return 0;
916 }
917
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700918 pr_debug("%s: session[%d]opcode[0x%x] token[0x%x]payload_s[%d] src[%d] dest[%d]\n",
919 __func__,
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700920 ac->session, data->opcode,
921 data->token, data->payload_size, data->src_port,
922 data->dest_port);
923 if ((data->opcode != ASM_DATA_EVENT_RENDERED_EOS) &&
924 (data->opcode != ASM_DATA_EVENT_EOS))
925 pr_debug("%s:Payload = [0x%x] status[0x%x]\n",
926 __func__, payload[0], payload[1]);
927 if (data->opcode == APR_BASIC_RSP_RESULT) {
928 token = data->token;
929 switch (payload[0]) {
930 case ASM_STREAM_CMD_SET_PP_PARAMS_V2:
931 if (rtac_make_asm_callback(ac->session, payload,
932 data->payload_size))
933 break;
934 case ASM_SESSION_CMD_PAUSE:
935 case ASM_DATA_CMD_EOS:
936 case ASM_STREAM_CMD_CLOSE:
937 case ASM_STREAM_CMD_FLUSH:
938 case ASM_SESSION_CMD_RUN_V2:
939 case ASM_SESSION_CMD_REGISTER_FORX_OVERFLOW_EVENTS:
940 case ASM_STREAM_CMD_FLUSH_READBUFS:
941 pr_debug("%s:Payload = [0x%x]\n", __func__, payload[0]);
942 if (token != ac->session) {
943 pr_err("%s:Invalid session[%d] rxed expected[%d]",
944 __func__, token, ac->session);
945 return -EINVAL;
946 }
947 case ASM_STREAM_CMD_OPEN_READ_V2:
948 case ASM_STREAM_CMD_OPEN_WRITE_V2:
949 case ASM_STREAM_CMD_OPEN_READWRITE_V2:
950 case ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2:
951 case ASM_STREAM_CMD_SET_ENCDEC_PARAM:
952 pr_debug("%s:Payload = [0x%x]stat[0x%x]\n",
953 __func__, payload[0], payload[1]);
954 if (atomic_read(&ac->cmd_state)) {
955 atomic_set(&ac->cmd_state, 0);
956 wake_up(&ac->cmd_wait);
957 }
958 if (ac->cb)
959 ac->cb(data->opcode, data->token,
960 (uint32_t *)data->payload, ac->priv);
961 break;
962 default:
963 pr_debug("%s:command[0x%x] not expecting rsp\n",
964 __func__, payload[0]);
965 break;
966 }
967 return 0;
968 }
969
970 switch (data->opcode) {
971 case ASM_DATA_EVENT_WRITE_DONE_V2:{
972 struct audio_port_data *port = &ac->port[IN];
973 pr_debug("%s: Rxed opcode[0x%x] status[0x%x] token[%d]",
974 __func__, payload[0], payload[1],
975 data->token);
976 if (ac->io_mode == SYNC_IO_MODE) {
977 if (port->buf == NULL) {
978 pr_err("%s: Unexpected Write Done\n",
979 __func__);
980 return -EINVAL;
981 }
982 spin_lock_irqsave(&port->dsp_lock, dsp_flags);
983 if (port->buf[data->token].phys !=
984 payload[0]) {
985 pr_err("Buf expected[%p]rxed[%p]\n",\
986 (void *)port->buf[data->token].phys,\
987 (void *)payload[0]);
988 spin_unlock_irqrestore(&port->dsp_lock,
989 dsp_flags);
990 return -EINVAL;
991 }
992 token = data->token;
993 port->buf[token].used = 1;
994 spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
995
996 config_debug_fs_write_cb();
997
998 for (i = 0; i < port->max_buf_cnt; i++)
999 pr_debug("%d ", port->buf[i].used);
1000
1001 }
1002 break;
1003 }
1004 case ASM_STREAM_CMDRSP_GET_PP_PARAMS_V2:
1005 rtac_make_asm_callback(ac->session, payload,
1006 data->payload_size);
1007 break;
1008 case ASM_DATA_EVENT_READ_DONE_V2:{
1009
1010 struct audio_port_data *port = &ac->port[OUT];
1011
1012 config_debug_fs_read_cb();
1013
1014 pr_debug("%s:R-D: status=%d buff_add=%x act_size=%d offset=%d\n",
1015 __func__, payload[READDONE_IDX_STATUS],
1016 payload[READDONE_IDX_BUFADD_LSW],
1017 payload[READDONE_IDX_SIZE],
1018 payload[READDONE_IDX_OFFSET]);
1019
1020 pr_debug("%s:R-D:msw_ts=%d lsw_ts=%d memmap_hdl=%x flags=%d id=%d num=%d\n",
1021 __func__, payload[READDONE_IDX_MSW_TS],
1022 payload[READDONE_IDX_LSW_TS],
1023 payload[READDONE_IDX_MEMMAP_HDL],
1024 payload[READDONE_IDX_FLAGS],
1025 payload[READDONE_IDX_SEQ_ID],
1026 payload[READDONE_IDX_NUMFRAMES]);
1027
1028 if (ac->io_mode == SYNC_IO_MODE) {
1029 if (port->buf == NULL) {
1030 pr_err("%s: Unexpected Write Done\n", __func__);
1031 return -EINVAL;
1032 }
1033 spin_lock_irqsave(&port->dsp_lock, dsp_flags);
1034 token = data->token;
1035 port->buf[token].used = 0;
1036 if (port->buf[token].phys !=
1037 payload[READDONE_IDX_BUFADD_LSW]) {
1038 pr_err("Buf expected[%p]rxed[%p]\n",\
1039 (void *)port->buf[token].phys,\
1040 (void *)payload[READDONE_IDX_BUFADD_LSW]);
1041 spin_unlock_irqrestore(&port->dsp_lock,
1042 dsp_flags);
1043 break;
1044 }
1045 port->buf[token].actual_size =
1046 payload[READDONE_IDX_SIZE];
1047 spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
1048 }
1049 break;
1050 }
1051 case ASM_DATA_EVENT_EOS:
1052 case ASM_DATA_EVENT_RENDERED_EOS:
1053 pr_debug("%s:EOS ACK received: rxed opcode[0x%x]\n",
1054 __func__, data->opcode);
1055 break;
1056 case ASM_SESSION_EVENTX_OVERFLOW:
1057 pr_err("ASM_SESSION_EVENTX_OVERFLOW\n");
1058 break;
1059 case ASM_SESSION_CMDRSP_GET_SESSIONTIME_V3:
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001060 pr_debug("%s: ASM_SESSION_CMDRSP_GET_SESSIONTIME_V3, payload[0] = %d, payload[1] = %d, payload[2] = %d\n",
1061 __func__,
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001062 payload[0], payload[1], payload[2]);
1063 ac->time_stamp = (uint64_t)(((uint64_t)payload[1] << 32) |
1064 payload[2]);
1065 if (atomic_read(&ac->cmd_state)) {
1066 atomic_set(&ac->cmd_state, 0);
1067 wake_up(&ac->cmd_wait);
1068 }
1069 break;
1070 case ASM_DATA_EVENT_SR_CM_CHANGE_NOTIFY:
1071 case ASM_DATA_EVENT_ENC_SR_CM_CHANGE_NOTIFY:
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001072 pr_debug("%s: ASM_DATA_EVENT_SR_CM_CHANGE_NOTIFY, payload[0] = %d, payload[1] = %d, payload[2] = %d, payload[3] = %d\n",
1073 __func__,
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001074 payload[0], payload[1], payload[2],
1075 payload[3]);
1076 break;
1077 }
1078 if (ac->cb)
1079 ac->cb(data->opcode, data->token,
1080 data->payload, ac->priv);
1081
1082 return 0;
1083}
1084
1085void *q6asm_is_cpu_buf_avail(int dir, struct audio_client *ac, uint32_t *size,
1086 uint32_t *index)
1087{
1088 void *data;
1089 unsigned char idx;
1090 struct audio_port_data *port;
1091
1092 if (!ac || ((dir != IN) && (dir != OUT)))
1093 return NULL;
1094
1095 if (ac->io_mode == SYNC_IO_MODE) {
1096 port = &ac->port[dir];
1097
1098 mutex_lock(&port->lock);
1099 idx = port->cpu_buf;
1100 if (port->buf == NULL) {
1101 pr_debug("%s:Buffer pointer null\n", __func__);
1102 mutex_unlock(&port->lock);
1103 return NULL;
1104 }
1105 /* dir 0: used = 0 means buf in use
1106 dir 1: used = 1 means buf in use */
1107 if (port->buf[idx].used == dir) {
1108 /* To make it more robust, we could loop and get the
1109 next avail buf, its risky though */
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001110 pr_debug("%s:Next buf idx[0x%x] not available, dir[%d]\n",
1111 __func__, idx, dir);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001112 mutex_unlock(&port->lock);
1113 return NULL;
1114 }
1115 *size = port->buf[idx].actual_size;
1116 *index = port->cpu_buf;
1117 data = port->buf[idx].data;
1118 pr_debug("%s:session[%d]index[%d] data[%p]size[%d]\n",
1119 __func__,
1120 ac->session,
1121 port->cpu_buf,
1122 data, *size);
1123 /* By default increase the cpu_buf cnt
1124 user accesses this function,increase cpu
1125 buf(to avoid another api)*/
1126 port->buf[idx].used = dir;
1127 port->cpu_buf = ((port->cpu_buf + 1) & (port->max_buf_cnt - 1));
1128 mutex_unlock(&port->lock);
1129 return data;
1130 }
1131 return NULL;
1132}
1133
1134void *q6asm_is_cpu_buf_avail_nolock(int dir, struct audio_client *ac,
1135 uint32_t *size, uint32_t *index)
1136{
1137 void *data;
1138 unsigned char idx;
1139 struct audio_port_data *port;
1140
1141 if (!ac || ((dir != IN) && (dir != OUT)))
1142 return NULL;
1143
1144 port = &ac->port[dir];
1145
1146 idx = port->cpu_buf;
1147 if (port->buf == NULL) {
1148 pr_debug("%s:Buffer pointer null\n", __func__);
1149 return NULL;
1150 }
1151 /*
1152 * dir 0: used = 0 means buf in use
1153 * dir 1: used = 1 means buf in use
1154 */
1155 if (port->buf[idx].used == dir) {
1156 /*
1157 * To make it more robust, we could loop and get the
1158 * next avail buf, its risky though
1159 */
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001160 pr_debug("%s:Next buf idx[0x%x] not available, dir[%d]\n",
1161 __func__, idx, dir);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001162 return NULL;
1163 }
1164 *size = port->buf[idx].actual_size;
1165 *index = port->cpu_buf;
1166 data = port->buf[idx].data;
1167 pr_debug("%s:session[%d]index[%d] data[%p]size[%d]\n",
1168 __func__, ac->session, port->cpu_buf,
1169 data, *size);
1170 /*
1171 * By default increase the cpu_buf cnt
1172 * user accesses this function,increase cpu
1173 * buf(to avoid another api)
1174 */
1175 port->buf[idx].used = dir;
1176 port->cpu_buf = ((port->cpu_buf + 1) & (port->max_buf_cnt - 1));
1177 return data;
1178}
1179
1180int q6asm_is_dsp_buf_avail(int dir, struct audio_client *ac)
1181{
1182 int ret = -1;
1183 struct audio_port_data *port;
1184 uint32_t idx;
1185
1186 if (!ac || (dir != OUT))
1187 return ret;
1188
1189 if (ac->io_mode == SYNC_IO_MODE) {
1190 port = &ac->port[dir];
1191
1192 mutex_lock(&port->lock);
1193 idx = port->dsp_buf;
1194
1195 if (port->buf[idx].used == (dir ^ 1)) {
1196 /* To make it more robust, we could loop and get the
1197 next avail buf, its risky though */
1198 pr_err("Next buf idx[0x%x] not available, dir[%d]\n",
1199 idx, dir);
1200 mutex_unlock(&port->lock);
1201 return ret;
1202 }
1203 pr_debug("%s: session[%d]dsp_buf=%d cpu_buf=%d\n", __func__,
1204 ac->session, port->dsp_buf, port->cpu_buf);
1205 ret = ((port->dsp_buf != port->cpu_buf) ? 0 : -1);
1206 mutex_unlock(&port->lock);
1207 }
1208 return ret;
1209}
1210
1211static void q6asm_add_hdr(struct audio_client *ac, struct apr_hdr *hdr,
1212 uint32_t pkt_size, uint32_t cmd_flg)
1213{
1214 pr_debug("%s:pkt_size=%d cmd_flg=%d session=%d\n", __func__, pkt_size,
1215 cmd_flg, ac->session);
1216 mutex_lock(&ac->cmd_lock);
1217 hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, \
1218 APR_HDR_LEN(sizeof(struct apr_hdr)),\
1219 APR_PKT_VER);
1220 hdr->src_svc = ((struct apr_svc *)ac->apr)->id;
1221 hdr->src_domain = APR_DOMAIN_APPS;
1222 hdr->dest_svc = APR_SVC_ASM;
1223 hdr->dest_domain = APR_DOMAIN_ADSP;
1224 hdr->src_port = ((ac->session << 8) & 0xFF00) | 0x01;
1225 hdr->dest_port = ((ac->session << 8) & 0xFF00) | 0x01;
1226 if (cmd_flg) {
1227 hdr->token = ac->session;
1228 atomic_set(&ac->cmd_state, 1);
1229 }
1230 hdr->pkt_size = pkt_size;
1231 mutex_unlock(&ac->cmd_lock);
1232 return;
1233}
1234
1235static void q6asm_add_hdr_async(struct audio_client *ac, struct apr_hdr *hdr,
1236 uint32_t pkt_size, uint32_t cmd_flg)
1237{
1238 pr_debug("pkt_size = %d, cmd_flg = %d, session = %d\n",
1239 pkt_size, cmd_flg, ac->session);
1240 hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, \
1241 APR_HDR_LEN(sizeof(struct apr_hdr)),\
1242 APR_PKT_VER);
1243 hdr->src_svc = ((struct apr_svc *)ac->apr)->id;
1244 hdr->src_domain = APR_DOMAIN_APPS;
1245 hdr->dest_svc = APR_SVC_ASM;
1246 hdr->dest_domain = APR_DOMAIN_ADSP;
1247 hdr->src_port = ((ac->session << 8) & 0xFF00) | 0x01;
1248 hdr->dest_port = ((ac->session << 8) & 0xFF00) | 0x01;
1249 if (cmd_flg) {
1250 hdr->token = ac->session;
1251 atomic_set(&ac->cmd_state, 1);
1252 }
1253 hdr->pkt_size = pkt_size;
1254 return;
1255}
1256
1257static void q6asm_add_mmaphdr(struct audio_client *ac, struct apr_hdr *hdr,
1258 u32 pkt_size, u32 cmd_flg, u32 token)
1259{
1260 pr_debug("%s:pkt size=%d cmd_flg=%d\n", __func__, pkt_size, cmd_flg);
1261 hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, \
1262 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
1263 hdr->src_port = 0;
1264 hdr->dest_port = 0;
1265 if (cmd_flg) {
1266 hdr->token = token;
1267 atomic_set(&ac->cmd_state, 1);
1268 }
1269 hdr->pkt_size = pkt_size;
1270 return;
1271}
1272int q6asm_open_read(struct audio_client *ac,
1273 uint32_t format)
1274{
1275 int rc = 0x00;
1276 struct asm_stream_cmd_open_read_v2 open;
1277
1278 uint16_t bits_per_sample = 16;
1279
1280
1281 config_debug_fs_reset_index();
1282
1283 if ((ac == NULL) || (ac->apr == NULL)) {
1284 pr_err("%s: APR handle NULL\n", __func__);
1285 return -EINVAL;
1286 }
1287 pr_debug("%s:session[%d]", __func__, ac->session);
1288
1289 q6asm_add_hdr(ac, &open.hdr, sizeof(open), TRUE);
1290 open.hdr.opcode = ASM_STREAM_CMD_OPEN_READ_V2;
1291 /* Stream prio : High, provide meta info with encoded frames */
1292 open.src_endpointype = ASM_END_POINT_DEVICE_MATRIX;
1293
1294 open.preprocopo_id = get_asm_topology();
1295 if (open.preprocopo_id == 0)
1296 open.preprocopo_id = ASM_STREAM_POSTPROC_TOPO_ID_DEFAULT;
1297 open.bits_per_sample = bits_per_sample;
1298
1299 switch (format) {
1300 case FORMAT_LINEAR_PCM:
1301 open.mode_flags = 0x00;
1302 open.enc_cfg_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V2;
1303 break;
1304 case FORMAT_MPEG4_AAC:
1305 open.mode_flags = BUFFER_META_ENABLE;
1306 open.enc_cfg_id = ASM_MEDIA_FMT_AAC_V2;
1307 break;
1308 case FORMAT_V13K:
1309 open.mode_flags = BUFFER_META_ENABLE;
1310 open.enc_cfg_id = ASM_MEDIA_FMT_V13K_FS;
1311 break;
1312 case FORMAT_EVRC:
1313 open.mode_flags = BUFFER_META_ENABLE;
1314 open.enc_cfg_id = ASM_MEDIA_FMT_EVRC_FS;
1315 break;
1316 case FORMAT_AMRNB:
1317 open.mode_flags = BUFFER_META_ENABLE ;
1318 open.enc_cfg_id = ASM_MEDIA_FMT_AMRNB_FS;
1319 break;
1320 case FORMAT_AMRWB:
1321 open.mode_flags = BUFFER_META_ENABLE ;
1322 open.enc_cfg_id = ASM_MEDIA_FMT_AMRWB_FS;
1323 break;
1324 default:
1325 pr_err("Invalid format[%d]\n", format);
1326 goto fail_cmd;
1327 }
1328 rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
1329 if (rc < 0) {
1330 pr_err("open failed op[0x%x]rc[%d]\n", \
1331 open.hdr.opcode, rc);
1332 goto fail_cmd;
1333 }
1334 rc = wait_event_timeout(ac->cmd_wait,
1335 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1336 if (!rc) {
1337 pr_err("%s: timeout. waited for open read rc[%d]\n", __func__,
1338 rc);
1339 goto fail_cmd;
1340 }
1341 return 0;
1342fail_cmd:
1343 return -EINVAL;
1344}
1345int q6asm_open_write(struct audio_client *ac, uint32_t format)
1346{
1347 int rc = 0x00;
1348 struct asm_stream_cmd_open_write_v2 open;
1349
1350 if ((ac == NULL) || (ac->apr == NULL)) {
1351 pr_err("%s: APR handle NULL\n", __func__);
1352 return -EINVAL;
1353 }
1354 pr_debug("%s: session[%d] wr_format[0x%x]", __func__, ac->session,
1355 format);
1356
1357 q6asm_add_hdr(ac, &open.hdr, sizeof(open), TRUE);
1358
1359 open.hdr.opcode = ASM_STREAM_CMD_OPEN_WRITE_V2;
1360 open.mode_flags = 0x00;
1361 /* source endpoint : matrix */
1362 open.sink_endpointype = ASM_END_POINT_DEVICE_MATRIX;
1363 open.bits_per_sample = 16;
1364
1365 open.postprocopo_id = get_asm_topology();
1366 if (open.postprocopo_id == 0)
1367 open.postprocopo_id = ASM_STREAM_POSTPROC_TOPO_ID_DEFAULT;
1368
1369 switch (format) {
1370 case FORMAT_LINEAR_PCM:
1371 open.dec_fmt_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V2;
1372 break;
1373 case FORMAT_MPEG4_AAC:
1374 open.dec_fmt_id = ASM_MEDIA_FMT_AAC_V2;
1375 break;
1376 case FORMAT_MPEG4_MULTI_AAC:
1377 open.dec_fmt_id = ASM_MEDIA_FMT_DOLBY_AAC;
1378 break;
1379 case FORMAT_WMA_V9:
1380 open.dec_fmt_id = ASM_MEDIA_FMT_WMA_V9_V2;
1381 break;
1382 case FORMAT_WMA_V10PRO:
1383 open.dec_fmt_id = ASM_MEDIA_FMT_WMA_V10PRO_V2;
1384 break;
1385 case FORMAT_MP3:
1386 open.dec_fmt_id = ASM_MEDIA_FMT_MP3;
1387 break;
1388 default:
1389 pr_err("%s: Invalid format[%d]\n", __func__, format);
1390 goto fail_cmd;
1391 }
1392 rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
1393 if (rc < 0) {
1394 pr_err("%s: open failed op[0x%x]rc[%d]\n", \
1395 __func__, open.hdr.opcode, rc);
1396 goto fail_cmd;
1397 }
1398 rc = wait_event_timeout(ac->cmd_wait,
1399 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1400 if (!rc) {
1401 pr_err("%s: timeout. waited for open write rc[%d]\n", __func__,
1402 rc);
1403 goto fail_cmd;
1404 }
1405 return 0;
1406fail_cmd:
1407 return -EINVAL;
1408}
1409
1410int q6asm_open_read_write(struct audio_client *ac,
1411 uint32_t rd_format,
1412 uint32_t wr_format)
1413{
1414 int rc = 0x00;
1415 struct asm_stream_cmd_open_readwrite_v2 open;
1416
1417 if ((ac == NULL) || (ac->apr == NULL)) {
1418 pr_err("APR handle NULL\n");
1419 return -EINVAL;
1420 }
1421 pr_debug("%s: session[%d]", __func__, ac->session);
1422 pr_debug("wr_format[0x%x]rd_format[0x%x]",
1423 wr_format, rd_format);
1424
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001425 ac->io_mode |= NT_MODE;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001426 q6asm_add_hdr(ac, &open.hdr, sizeof(open), TRUE);
1427 open.hdr.opcode = ASM_STREAM_CMD_OPEN_READWRITE_V2;
1428
1429 open.mode_flags = BUFFER_META_ENABLE;
1430 open.bits_per_sample = 16;
1431 /* source endpoint : matrix */
1432 open.postprocopo_id = get_asm_topology();
1433 if (open.postprocopo_id == 0)
1434 open.postprocopo_id = ASM_STREAM_POSTPROC_TOPO_ID_DEFAULT;
1435
1436 switch (wr_format) {
1437 case FORMAT_LINEAR_PCM:
1438 open.dec_fmt_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V2;
1439 break;
1440 case FORMAT_MPEG4_AAC:
1441 open.dec_fmt_id = ASM_MEDIA_FMT_AAC_V2;
1442 break;
1443 case FORMAT_MPEG4_MULTI_AAC:
1444 open.dec_fmt_id = ASM_MEDIA_FMT_DOLBY_AAC;
1445 break;
1446 case FORMAT_WMA_V9:
1447 open.dec_fmt_id = ASM_MEDIA_FMT_WMA_V9_V2;
1448 break;
1449 case FORMAT_WMA_V10PRO:
1450 open.dec_fmt_id = ASM_MEDIA_FMT_WMA_V10PRO_V2;
1451 break;
1452 case FORMAT_AMRNB:
1453 open.dec_fmt_id = ASM_MEDIA_FMT_AMRNB_FS;
1454 break;
1455 case FORMAT_AMRWB:
1456 open.dec_fmt_id = ASM_MEDIA_FMT_AMRWB_FS;
1457 break;
1458 case FORMAT_V13K:
1459 open.dec_fmt_id = ASM_MEDIA_FMT_V13K_FS;
1460 break;
1461 case FORMAT_EVRC:
1462 open.dec_fmt_id = ASM_MEDIA_FMT_EVRC_FS;
1463 break;
1464 case FORMAT_EVRCB:
1465 open.dec_fmt_id = ASM_MEDIA_FMT_EVRCB_FS;
1466 break;
1467 case FORMAT_EVRCWB:
1468 open.dec_fmt_id = ASM_MEDIA_FMT_EVRCWB_FS;
1469 break;
1470 case FORMAT_MP3:
1471 open.dec_fmt_id = ASM_MEDIA_FMT_MP3;
1472 break;
1473 default:
1474 pr_err("Invalid format[%d]\n", wr_format);
1475 goto fail_cmd;
1476 }
1477
1478 switch (rd_format) {
1479 case FORMAT_LINEAR_PCM:
1480 open.enc_cfg_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V2;
1481 break;
1482 case FORMAT_MPEG4_AAC:
1483 open.enc_cfg_id = ASM_MEDIA_FMT_AAC_V2;
1484 break;
1485 case FORMAT_V13K:
1486 open.enc_cfg_id = ASM_MEDIA_FMT_V13K_FS;
1487 break;
1488 case FORMAT_EVRC:
1489 open.enc_cfg_id = ASM_MEDIA_FMT_EVRC_FS;
1490 break;
1491 case FORMAT_AMRNB:
1492 open.enc_cfg_id = ASM_MEDIA_FMT_AMRNB_FS;
1493 break;
1494 case FORMAT_AMRWB:
1495 open.enc_cfg_id = ASM_MEDIA_FMT_AMRWB_FS;
1496 break;
1497 default:
1498 pr_err("Invalid format[%d]\n", rd_format);
1499 goto fail_cmd;
1500 }
1501 pr_debug("%s:rdformat[0x%x]wrformat[0x%x]\n", __func__,
1502 open.enc_cfg_id, open.dec_fmt_id);
1503
1504 rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
1505 if (rc < 0) {
1506 pr_err("open failed op[0x%x]rc[%d]\n", \
1507 open.hdr.opcode, rc);
1508 goto fail_cmd;
1509 }
1510 rc = wait_event_timeout(ac->cmd_wait,
1511 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1512 if (!rc) {
1513 pr_err("timeout. waited for open read-write rc[%d]\n", rc);
1514 goto fail_cmd;
1515 }
1516 return 0;
1517fail_cmd:
1518 return -EINVAL;
1519}
1520
1521int q6asm_run(struct audio_client *ac, uint32_t flags,
1522 uint32_t msw_ts, uint32_t lsw_ts)
1523{
1524 struct asm_session_cmd_run_v2 run;
1525 int rc;
1526 if (!ac || ac->apr == NULL) {
1527 pr_err("APR handle NULL\n");
1528 return -EINVAL;
1529 }
1530 pr_debug("%s session[%d]", __func__, ac->session);
1531 q6asm_add_hdr(ac, &run.hdr, sizeof(run), TRUE);
1532
1533 run.hdr.opcode = ASM_SESSION_CMD_RUN_V2;
1534 run.flags = flags;
1535 run.time_lsw = lsw_ts;
1536 run.time_msw = msw_ts;
1537
1538 config_debug_fs_run();
1539
1540 rc = apr_send_pkt(ac->apr, (uint32_t *) &run);
1541 if (rc < 0) {
1542 pr_err("Commmand run failed[%d]", rc);
1543 goto fail_cmd;
1544 }
1545
1546 rc = wait_event_timeout(ac->cmd_wait,
1547 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1548 if (!rc) {
1549 pr_err("timeout. waited for run success rc[%d]", rc);
1550 goto fail_cmd;
1551 }
1552
1553 return 0;
1554fail_cmd:
1555 return -EINVAL;
1556}
1557
1558int q6asm_run_nowait(struct audio_client *ac, uint32_t flags,
1559 uint32_t msw_ts, uint32_t lsw_ts)
1560{
1561 struct asm_session_cmd_run_v2 run;
1562 int rc;
1563 if (!ac || ac->apr == NULL) {
1564 pr_err("%s:APR handle NULL\n", __func__);
1565 return -EINVAL;
1566 }
1567 pr_debug("session[%d]", ac->session);
1568 q6asm_add_hdr_async(ac, &run.hdr, sizeof(run), TRUE);
1569
1570 run.hdr.opcode = ASM_SESSION_CMD_RUN_V2;
1571 run.flags = flags;
1572 run.time_lsw = lsw_ts;
1573 run.time_msw = msw_ts;
1574
1575 rc = apr_send_pkt(ac->apr, (uint32_t *) &run);
1576 if (rc < 0) {
1577 pr_err("%s:Commmand run failed[%d]", __func__, rc);
1578 return -EINVAL;
1579 }
1580 return 0;
1581}
1582
1583
1584int q6asm_enc_cfg_blk_aac(struct audio_client *ac,
1585 uint32_t frames_per_buf,
1586 uint32_t sample_rate, uint32_t channels,
1587 uint32_t bit_rate, uint32_t mode, uint32_t format)
1588{
1589 struct asm_aac_enc_cfg_v2 enc_cfg;
1590 int rc = 0;
1591
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001592 pr_debug("%s:session[%d]frames[%d]SR[%d]ch[%d]bitrate[%d]mode[%d] format[%d]",
1593 __func__, ac->session, frames_per_buf,
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001594 sample_rate, channels, bit_rate, mode, format);
1595
1596 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
1597
1598 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1599 enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
1600 enc_cfg.encdec.param_size = sizeof(struct asm_aac_enc_cfg_v2) -
1601 sizeof(struct asm_stream_cmd_set_encdec_param);
1602 enc_cfg.encblk.frames_per_buf = frames_per_buf;
1603 enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
1604 sizeof(struct asm_enc_cfg_blk_param_v2);
1605 enc_cfg.bit_rate = bit_rate;
1606 enc_cfg.enc_mode = mode;
1607 enc_cfg.aac_fmt_flag = format;
1608 enc_cfg.channel_cfg = channels;
1609 enc_cfg.sample_rate = sample_rate;
1610
1611 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
1612 if (rc < 0) {
1613 pr_err("Comamnd %d failed\n", ASM_STREAM_CMD_SET_ENCDEC_PARAM);
1614 rc = -EINVAL;
1615 goto fail_cmd;
1616 }
1617 rc = wait_event_timeout(ac->cmd_wait,
1618 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1619 if (!rc) {
1620 pr_err("timeout. waited for FORMAT_UPDATE\n");
1621 goto fail_cmd;
1622 }
1623 return 0;
1624fail_cmd:
1625 return -EINVAL;
1626}
1627
1628int q6asm_set_encdec_chan_map(struct audio_client *ac,
1629 uint32_t num_channels)
1630{
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001631 struct asm_dec_out_chan_map_param chan_map;
1632 u8 *channel_mapping;
1633 int rc = 0;
1634 pr_debug("%s: Session %d, num_channels = %d\n",
1635 __func__, ac->session, num_channels);
1636 q6asm_add_hdr(ac, &chan_map.hdr, sizeof(chan_map), TRUE);
1637 chan_map.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1638 chan_map.encdec.param_id = ASM_PARAM_ID_DEC_OUTPUT_CHAN_MAP;
1639 chan_map.encdec.param_size = sizeof(struct asm_dec_out_chan_map_param) -
1640 (sizeof(struct apr_hdr) +
1641 sizeof(struct asm_stream_cmd_set_encdec_param));
1642 chan_map.num_channels = num_channels;
1643 channel_mapping = chan_map.channel_mapping;
1644 memset(channel_mapping, PCM_CHANNEL_NULL, MAX_CHAN_MAP_CHANNELS);
1645 if (q6asm_map_channels(channel_mapping, num_channels))
1646 return -EINVAL;
1647
1648 rc = apr_send_pkt(ac->apr, (uint32_t *) &chan_map);
1649 if (rc < 0) {
1650 pr_err("%s:Command opcode[0x%x]paramid[0x%x] failed\n",
1651 __func__, ASM_STREAM_CMD_SET_ENCDEC_PARAM,
1652 ASM_PARAM_ID_DEC_OUTPUT_CHAN_MAP);
1653 goto fail_cmd;
1654 }
1655 rc = wait_event_timeout(ac->cmd_wait,
1656 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1657 if (!rc) {
1658 pr_err("%s:timeout opcode[0x%x]\n", __func__,
1659 chan_map.hdr.opcode);
1660 rc = -ETIMEDOUT;
1661 goto fail_cmd;
1662 }
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001663 return 0;
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001664fail_cmd:
1665 return rc;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001666}
1667
1668int q6asm_enc_cfg_blk_pcm(struct audio_client *ac,
1669 uint32_t rate, uint32_t channels)
1670{
1671 struct asm_multi_channel_pcm_enc_cfg_v2 enc_cfg;
1672 u8 *channel_mapping;
1673 u32 frames_per_buf = 0;
1674
1675 int rc = 0;
1676
1677 pr_debug("%s: Session %d, rate = %d, channels = %d\n", __func__,
1678 ac->session, rate, channels);
1679
1680 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
1681 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1682 enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
1683 enc_cfg.encdec.param_size = sizeof(enc_cfg) - sizeof(enc_cfg.hdr) -
1684 sizeof(enc_cfg.encdec);
1685 enc_cfg.encblk.frames_per_buf = frames_per_buf;
1686 enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
1687 sizeof(struct asm_enc_cfg_blk_param_v2);
1688
1689 enc_cfg.num_channels = channels;
1690 enc_cfg.bits_per_sample = 16;
1691 enc_cfg.sample_rate = rate;
1692 enc_cfg.is_signed = 1;
1693 channel_mapping = enc_cfg.channel_mapping; /* ??? PHANI */
1694
1695 memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
1696
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001697 if (q6asm_map_channels(channel_mapping, channels))
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001698 return -EINVAL;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001699
1700 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
1701 if (rc < 0) {
1702 pr_err("Comamnd open failed\n");
1703 rc = -EINVAL;
1704 goto fail_cmd;
1705 }
1706 rc = wait_event_timeout(ac->cmd_wait,
1707 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1708 if (!rc) {
1709 pr_err("timeout opcode[0x%x] ", enc_cfg.hdr.opcode);
1710 goto fail_cmd;
1711 }
1712 return 0;
1713fail_cmd:
1714 return -EINVAL;
1715}
1716
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001717int q6asm_enc_cfg_blk_pcm_native(struct audio_client *ac,
1718 uint32_t rate, uint32_t channels)
1719{
1720 struct asm_multi_channel_pcm_enc_cfg_v2 enc_cfg;
1721 u8 *channel_mapping;
1722 u32 frames_per_buf = 0;
1723
1724 int rc = 0;
1725
1726 pr_debug("%s: Session %d, rate = %d, channels = %d\n", __func__,
1727 ac->session, rate, channels);
1728
1729 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
1730
1731 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1732 enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
1733 enc_cfg.encdec.param_size = sizeof(enc_cfg) - sizeof(enc_cfg.hdr) -
1734 sizeof(enc_cfg.encdec);
1735 enc_cfg.encblk.frames_per_buf = frames_per_buf;
1736 enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
1737 sizeof(struct asm_enc_cfg_blk_param_v2);
1738
1739 enc_cfg.num_channels = 0;/*channels;*/
1740 enc_cfg.bits_per_sample = 16;
1741 enc_cfg.sample_rate = 0;/*rate;*/
1742 enc_cfg.is_signed = 1;
1743 channel_mapping = enc_cfg.channel_mapping; /* ??? PHANI */
1744
1745 memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
1746
1747 if (q6asm_map_channels(channel_mapping, channels))
1748 return -EINVAL;
1749
1750 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
1751 if (rc < 0) {
1752 pr_err("Comamnd open failed\n");
1753 rc = -EINVAL;
1754 goto fail_cmd;
1755 }
1756 rc = wait_event_timeout(ac->cmd_wait,
1757 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1758 if (!rc) {
1759 pr_err("timeout opcode[0x%x] ", enc_cfg.hdr.opcode);
1760 goto fail_cmd;
1761 }
1762 return 0;
1763fail_cmd:
1764 return -EINVAL;
1765}
1766
1767static int q6asm_map_channels(u8 *channel_mapping, uint32_t channels)
1768{
1769 u8 *lchannel_mapping;
1770 lchannel_mapping = channel_mapping;
1771 pr_debug("%s channels passed: %d\n", __func__, channels);
1772 if (channels == 1) {
1773 lchannel_mapping[0] = PCM_CHANNEL_FC;
1774 } else if (channels == 2) {
1775 lchannel_mapping[0] = PCM_CHANNEL_FL;
1776 lchannel_mapping[1] = PCM_CHANNEL_FR;
1777 } else if (channels == 3) {
1778 lchannel_mapping[0] = PCM_CHANNEL_FC;
1779 lchannel_mapping[1] = PCM_CHANNEL_FL;
1780 lchannel_mapping[2] = PCM_CHANNEL_FR;
1781 } else if (channels == 4) {
1782 lchannel_mapping[0] = PCM_CHANNEL_FC;
1783 lchannel_mapping[1] = PCM_CHANNEL_FL;
1784 lchannel_mapping[2] = PCM_CHANNEL_FR;
1785 lchannel_mapping[3] = PCM_CHANNEL_LB;
1786 } else if (channels == 5) {
1787 lchannel_mapping[0] = PCM_CHANNEL_FC;
1788 lchannel_mapping[1] = PCM_CHANNEL_FL;
1789 lchannel_mapping[2] = PCM_CHANNEL_FR;
1790 lchannel_mapping[3] = PCM_CHANNEL_LB;
1791 lchannel_mapping[4] = PCM_CHANNEL_RB;
1792 } else if (channels == 6) {
1793 lchannel_mapping[0] = PCM_CHANNEL_FC;
1794 lchannel_mapping[1] = PCM_CHANNEL_FL;
1795 lchannel_mapping[2] = PCM_CHANNEL_FR;
1796 lchannel_mapping[3] = PCM_CHANNEL_LB;
1797 lchannel_mapping[4] = PCM_CHANNEL_RB;
1798 lchannel_mapping[5] = PCM_CHANNEL_LFE;
1799 } else {
1800 pr_err("%s: ERROR.unsupported num_ch = %u\n",
1801 __func__, channels);
1802 return -EINVAL;
1803 }
1804 return 0;
1805}
1806
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001807int q6asm_enable_sbrps(struct audio_client *ac,
1808 uint32_t sbr_ps_enable)
1809{
1810 struct asm_aac_sbr_ps_flag_param sbrps;
1811 u32 frames_per_buf = 0;
1812
1813 int rc = 0;
1814
1815 pr_debug("%s: Session %d\n", __func__, ac->session);
1816
1817 q6asm_add_hdr(ac, &sbrps.hdr, sizeof(sbrps), TRUE);
1818
1819 sbrps.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1820 sbrps.encdec.param_id = ASM_PARAM_ID_AAC_SBR_PS_FLAG;
1821 sbrps.encdec.param_size = sizeof(struct asm_aac_sbr_ps_flag_param) -
1822 sizeof(struct asm_stream_cmd_set_encdec_param);
1823 sbrps.encblk.frames_per_buf = frames_per_buf;
1824 sbrps.encblk.enc_cfg_blk_size = sbrps.encdec.param_size -
1825 sizeof(struct asm_enc_cfg_blk_param_v2);
1826
1827 sbrps.sbr_ps_flag = sbr_ps_enable;
1828
1829 rc = apr_send_pkt(ac->apr, (uint32_t *) &sbrps);
1830 if (rc < 0) {
1831 pr_err("Command opcode[0x%x]paramid[0x%x] failed\n",
1832 ASM_STREAM_CMD_SET_ENCDEC_PARAM,
1833 ASM_PARAM_ID_AAC_SBR_PS_FLAG);
1834 rc = -EINVAL;
1835 goto fail_cmd;
1836 }
1837 rc = wait_event_timeout(ac->cmd_wait,
1838 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1839 if (!rc) {
1840 pr_err("timeout opcode[0x%x] ", sbrps.hdr.opcode);
1841 goto fail_cmd;
1842 }
1843 return 0;
1844fail_cmd:
1845 return -EINVAL;
1846}
1847
1848int q6asm_cfg_dual_mono_aac(struct audio_client *ac,
1849 uint16_t sce_left, uint16_t sce_right)
1850{
1851 struct asm_aac_dual_mono_mapping_param dual_mono;
1852 u32 frames_per_buf = 0;
1853
1854 int rc = 0;
1855
1856 pr_debug("%s: Session %d, sce_left = %d, sce_right = %d\n",
1857 __func__, ac->session, sce_left, sce_right);
1858
1859 q6asm_add_hdr(ac, &dual_mono.hdr, sizeof(dual_mono), TRUE);
1860
1861 dual_mono.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1862 dual_mono.encdec.param_id = ASM_PARAM_ID_AAC_DUAL_MONO_MAPPING;
1863 dual_mono.encdec.param_size = sizeof(struct asm_aac_enc_cfg_v2) -
1864 sizeof(struct asm_stream_cmd_set_encdec_param);
1865 dual_mono.encblk.frames_per_buf = frames_per_buf;
1866 dual_mono.encblk.enc_cfg_blk_size = dual_mono.encdec.param_size -
1867 sizeof(struct asm_enc_cfg_blk_param_v2);
1868 dual_mono.left_channel_sce = sce_left;
1869 dual_mono.right_channel_sce = sce_right;
1870
1871 rc = apr_send_pkt(ac->apr, (uint32_t *) &dual_mono);
1872 if (rc < 0) {
1873 pr_err("%s:Command opcode[0x%x]paramid[0x%x] failed\n",
1874 __func__, ASM_STREAM_CMD_SET_ENCDEC_PARAM,
1875 ASM_PARAM_ID_AAC_DUAL_MONO_MAPPING);
1876 rc = -EINVAL;
1877 goto fail_cmd;
1878 }
1879 rc = wait_event_timeout(ac->cmd_wait,
1880 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1881 if (!rc) {
1882 pr_err("%s:timeout opcode[0x%x]\n", __func__,
1883 dual_mono.hdr.opcode);
1884 goto fail_cmd;
1885 }
1886 return 0;
1887fail_cmd:
1888 return -EINVAL;
1889}
1890
1891int q6asm_enc_cfg_blk_qcelp(struct audio_client *ac, uint32_t frames_per_buf,
1892 uint16_t min_rate, uint16_t max_rate,
1893 uint16_t reduced_rate_level, uint16_t rate_modulation_cmd)
1894{
1895 struct asm_v13k_enc_cfg enc_cfg;
1896 int rc = 0;
1897
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001898 pr_debug("%s:session[%d]frames[%d]min_rate[0x%4x]max_rate[0x%4x] reduced_rate_level[0x%4x]rate_modulation_cmd[0x%4x]",
1899 __func__,
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001900 ac->session, frames_per_buf, min_rate, max_rate,
1901 reduced_rate_level, rate_modulation_cmd);
1902
1903 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
1904 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1905 enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
1906 enc_cfg.encdec.param_size = sizeof(struct asm_v13k_enc_cfg) -
1907 sizeof(struct asm_stream_cmd_set_encdec_param);
1908 enc_cfg.encblk.frames_per_buf = frames_per_buf;
1909 enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
1910 sizeof(struct asm_enc_cfg_blk_param_v2);
1911
1912 enc_cfg.min_rate = min_rate;
1913 enc_cfg.max_rate = max_rate;
1914 enc_cfg.reduced_rate_cmd = reduced_rate_level;
1915 enc_cfg.rate_mod_cmd = rate_modulation_cmd;
1916
1917 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
1918 if (rc < 0) {
1919 pr_err("Comamnd %d failed\n", ASM_STREAM_CMD_SET_ENCDEC_PARAM);
1920 goto fail_cmd;
1921 }
1922 rc = wait_event_timeout(ac->cmd_wait,
1923 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1924 if (!rc) {
1925 pr_err("timeout. waited for setencdec v13k resp\n");
1926 goto fail_cmd;
1927 }
1928 return 0;
1929fail_cmd:
1930 return -EINVAL;
1931}
1932
1933int q6asm_enc_cfg_blk_evrc(struct audio_client *ac, uint32_t frames_per_buf,
1934 uint16_t min_rate, uint16_t max_rate,
1935 uint16_t rate_modulation_cmd)
1936{
1937 struct asm_evrc_enc_cfg enc_cfg;
1938 int rc = 0;
1939
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001940 pr_debug("%s:session[%d]frames[%d]min_rate[0x%4x]max_rate[0x%4x] rate_modulation_cmd[0x%4x]",
1941 __func__, ac->session,
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001942 frames_per_buf, min_rate, max_rate, rate_modulation_cmd);
1943
1944 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
1945 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1946 enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
1947 enc_cfg.encdec.param_size = sizeof(struct asm_evrc_enc_cfg) -
1948 sizeof(struct asm_stream_cmd_set_encdec_param);
1949 enc_cfg.encblk.frames_per_buf = frames_per_buf;
1950 enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
1951 sizeof(struct asm_enc_cfg_blk_param_v2);
1952
1953 enc_cfg.min_rate = min_rate;
1954 enc_cfg.max_rate = max_rate;
1955 enc_cfg.rate_mod_cmd = rate_modulation_cmd;
1956 enc_cfg.reserved = 0;
1957
1958 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
1959 if (rc < 0) {
1960 pr_err("Comamnd %d failed\n", ASM_STREAM_CMD_SET_ENCDEC_PARAM);
1961 goto fail_cmd;
1962 }
1963 rc = wait_event_timeout(ac->cmd_wait,
1964 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1965 if (!rc) {
1966 pr_err("timeout. waited for encdec evrc\n");
1967 goto fail_cmd;
1968 }
1969 return 0;
1970fail_cmd:
1971 return -EINVAL;
1972}
1973
1974int q6asm_enc_cfg_blk_amrnb(struct audio_client *ac, uint32_t frames_per_buf,
1975 uint16_t band_mode, uint16_t dtx_enable)
1976{
1977 struct asm_amrnb_enc_cfg enc_cfg;
1978 int rc = 0;
1979
1980 pr_debug("%s:session[%d]frames[%d]band_mode[0x%4x]dtx_enable[0x%4x]",
1981 __func__, ac->session, frames_per_buf, band_mode, dtx_enable);
1982
1983 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
1984 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1985 enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
1986 enc_cfg.encdec.param_size = sizeof(struct asm_amrnb_enc_cfg) -
1987 sizeof(struct asm_stream_cmd_set_encdec_param);
1988 enc_cfg.encblk.frames_per_buf = frames_per_buf;
1989 enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
1990 sizeof(struct asm_enc_cfg_blk_param_v2);
1991
1992 enc_cfg.enc_mode = band_mode;
1993 enc_cfg.dtx_mode = dtx_enable;
1994
1995 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
1996 if (rc < 0) {
1997 pr_err("Comamnd %d failed\n", ASM_STREAM_CMD_SET_ENCDEC_PARAM);
1998 goto fail_cmd;
1999 }
2000 rc = wait_event_timeout(ac->cmd_wait,
2001 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2002 if (!rc) {
2003 pr_err("timeout. waited for set encdec amrnb\n");
2004 goto fail_cmd;
2005 }
2006 return 0;
2007fail_cmd:
2008 return -EINVAL;
2009}
2010
2011int q6asm_enc_cfg_blk_amrwb(struct audio_client *ac, uint32_t frames_per_buf,
2012 uint16_t band_mode, uint16_t dtx_enable)
2013{
2014 struct asm_amrwb_enc_cfg enc_cfg;
2015 int rc = 0;
2016
2017 pr_debug("%s:session[%d]frames[%d]band_mode[0x%4x]dtx_enable[0x%4x]",
2018 __func__, ac->session, frames_per_buf, band_mode, dtx_enable);
2019
2020 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
2021 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
2022 enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
2023 enc_cfg.encdec.param_size = sizeof(struct asm_amrwb_enc_cfg) -
2024 sizeof(struct asm_stream_cmd_set_encdec_param);
2025 enc_cfg.encblk.frames_per_buf = frames_per_buf;
2026 enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
2027 sizeof(struct asm_enc_cfg_blk_param_v2);
2028
2029 enc_cfg.enc_mode = band_mode;
2030 enc_cfg.dtx_mode = dtx_enable;
2031
2032 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
2033 if (rc < 0) {
2034 pr_err("Comamnd %d failed\n", ASM_STREAM_CMD_SET_ENCDEC_PARAM);
2035 goto fail_cmd;
2036 }
2037 rc = wait_event_timeout(ac->cmd_wait,
2038 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2039 if (!rc) {
2040 pr_err("timeout. waited for FORMAT_UPDATE\n");
2041 goto fail_cmd;
2042 }
2043 return 0;
2044fail_cmd:
2045 return -EINVAL;
2046}
2047
2048
2049int q6asm_media_format_block_aac(struct audio_client *ac,
2050 struct asm_aac_cfg *cfg)
2051{
2052 return q6asm_media_format_block_multi_aac(ac, cfg);
2053}
2054
2055int q6asm_media_format_block_pcm(struct audio_client *ac,
2056 uint32_t rate, uint32_t channels)
2057{
2058 struct asm_multi_channel_pcm_fmt_blk_v2 fmt;
2059 u8 *channel_mapping;
2060 int rc = 0;
2061
2062 pr_debug("%s:session[%d]rate[%d]ch[%d]\n", __func__, ac->session, rate,
2063 channels);
2064
2065 q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
2066
2067 fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
2068 fmt.fmt_blk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
2069 sizeof(fmt.fmt_blk);
2070 fmt.num_channels = channels;
2071 fmt.bits_per_sample = 16;
2072 fmt.sample_rate = rate;
2073 fmt.is_signed = 1;
2074
2075 channel_mapping = fmt.channel_mapping;
2076
2077 memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
2078
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002079 if (q6asm_map_channels(channel_mapping, channels))
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002080 return -EINVAL;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002081
2082 rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
2083 if (rc < 0) {
2084 pr_err("%s:Comamnd open failed\n", __func__);
2085 goto fail_cmd;
2086 }
2087 rc = wait_event_timeout(ac->cmd_wait,
2088 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2089 if (!rc) {
2090 pr_err("%s:timeout. waited for format update\n", __func__);
2091 goto fail_cmd;
2092 }
2093 return 0;
2094fail_cmd:
2095 return -EINVAL;
2096}
2097
2098int q6asm_media_format_block_multi_aac(struct audio_client *ac,
2099 struct asm_aac_cfg *cfg)
2100{
2101 struct asm_aac_fmt_blk_v2 fmt;
2102 int rc = 0;
2103
2104 pr_debug("%s:session[%d]rate[%d]ch[%d]\n", __func__, ac->session,
2105 cfg->sample_rate, cfg->ch_cfg);
2106
2107 q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
2108
2109 fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
2110 fmt.fmt_blk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
2111 sizeof(fmt.fmt_blk);
2112 fmt.aac_fmt_flag = cfg->format;
2113 fmt.audio_objype = cfg->aot;
2114 /* If zero, PCE is assumed to be available in bitstream*/
2115 fmt.total_size_of_PCE_bits = 0;
2116 fmt.channel_config = cfg->ch_cfg;
2117 fmt.sample_rate = cfg->sample_rate;
2118
2119 pr_info("%s:format=%x cfg_size=%d aac-cfg=%x aot=%d ch=%d sr=%d\n",
2120 __func__, fmt.aac_fmt_flag, fmt.fmt_blk.fmt_blk_size,
2121 fmt.aac_fmt_flag,
2122 fmt.audio_objype,
2123 fmt.channel_config,
2124 fmt.sample_rate);
2125 rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
2126 if (rc < 0) {
2127 pr_err("%s:Comamnd open failed\n", __func__);
2128 goto fail_cmd;
2129 }
2130 rc = wait_event_timeout(ac->cmd_wait,
2131 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2132 if (!rc) {
2133 pr_err("%s:timeout. waited for FORMAT_UPDATE\n", __func__);
2134 goto fail_cmd;
2135 }
2136 return 0;
2137fail_cmd:
2138 return -EINVAL;
2139}
2140
2141int q6asm_media_format_block_wma(struct audio_client *ac,
2142 void *cfg)
2143{
2144 struct asm_wmastdv9_fmt_blk_v2 fmt;
2145 struct asm_wma_cfg *wma_cfg = (struct asm_wma_cfg *)cfg;
2146 int rc = 0;
2147
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002148 pr_debug("session[%d]format_tag[0x%4x] rate[%d] ch[0x%4x] bps[%d], balign[0x%4x], bit_sample[0x%4x], ch_msk[%d], enc_opt[0x%4x]\n",
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002149 ac->session, wma_cfg->format_tag, wma_cfg->sample_rate,
2150 wma_cfg->ch_cfg, wma_cfg->avg_bytes_per_sec,
2151 wma_cfg->block_align, wma_cfg->valid_bits_per_sample,
2152 wma_cfg->ch_mask, wma_cfg->encode_opt);
2153
2154 q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
2155
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002156 fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
2157 fmt.fmtblk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
2158 sizeof(fmt.fmtblk);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002159 fmt.fmtag = wma_cfg->format_tag;
2160 fmt.num_channels = wma_cfg->ch_cfg;
2161 fmt.sample_rate = wma_cfg->sample_rate;
2162 fmt.avg_bytes_per_sec = wma_cfg->avg_bytes_per_sec;
2163 fmt.blk_align = wma_cfg->block_align;
2164 fmt.bits_per_sample =
2165 wma_cfg->valid_bits_per_sample;
2166 fmt.channel_mask = wma_cfg->ch_mask;
2167 fmt.enc_options = wma_cfg->encode_opt;
2168
2169 rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
2170 if (rc < 0) {
2171 pr_err("%s:Comamnd open failed\n", __func__);
2172 goto fail_cmd;
2173 }
2174 rc = wait_event_timeout(ac->cmd_wait,
2175 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2176 if (!rc) {
2177 pr_err("%s:timeout. waited for FORMAT_UPDATE\n", __func__);
2178 goto fail_cmd;
2179 }
2180 return 0;
2181fail_cmd:
2182 return -EINVAL;
2183}
2184
2185int q6asm_media_format_block_wmapro(struct audio_client *ac,
2186 void *cfg)
2187{
2188 struct asm_wmaprov10_fmt_blk_v2 fmt;
2189 struct asm_wmapro_cfg *wmapro_cfg = (struct asm_wmapro_cfg *)cfg;
2190 int rc = 0;
2191
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002192 pr_debug("session[%d]format_tag[0x%4x] rate[%d] ch[0x%4x] bps[%d], balign[0x%4x], bit_sample[0x%4x], ch_msk[%d], enc_opt[0x%4x], adv_enc_opt[0x%4x], adv_enc_opt2[0x%8x]\n",
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002193 ac->session, wmapro_cfg->format_tag, wmapro_cfg->sample_rate,
2194 wmapro_cfg->ch_cfg, wmapro_cfg->avg_bytes_per_sec,
2195 wmapro_cfg->block_align, wmapro_cfg->valid_bits_per_sample,
2196 wmapro_cfg->ch_mask, wmapro_cfg->encode_opt,
2197 wmapro_cfg->adv_encode_opt, wmapro_cfg->adv_encode_opt2);
2198
2199 q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
2200
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002201 fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
2202 fmt.fmtblk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
2203 sizeof(fmt.fmtblk);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002204
2205 fmt.fmtag = wmapro_cfg->format_tag;
2206 fmt.num_channels = wmapro_cfg->ch_cfg;
2207 fmt.sample_rate = wmapro_cfg->sample_rate;
2208 fmt.avg_bytes_per_sec =
2209 wmapro_cfg->avg_bytes_per_sec;
2210 fmt.blk_align = wmapro_cfg->block_align;
2211 fmt.bits_per_sample = wmapro_cfg->valid_bits_per_sample;
2212 fmt.channel_mask = wmapro_cfg->ch_mask;
2213 fmt.enc_options = wmapro_cfg->encode_opt;
2214 fmt.usAdvancedEncodeOpt = wmapro_cfg->adv_encode_opt;
2215 fmt.advanced_enc_options2 = wmapro_cfg->adv_encode_opt2;
2216
2217 rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
2218 if (rc < 0) {
2219 pr_err("%s:Comamnd open failed\n", __func__);
2220 goto fail_cmd;
2221 }
2222 rc = wait_event_timeout(ac->cmd_wait,
2223 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2224 if (!rc) {
2225 pr_err("%s:timeout. waited for FORMAT_UPDATE\n", __func__);
2226 goto fail_cmd;
2227 }
2228 return 0;
2229fail_cmd:
2230 return -EINVAL;
2231}
2232
2233int q6asm_memory_map(struct audio_client *ac, uint32_t buf_add, int dir,
2234 uint32_t bufsz, uint32_t bufcnt)
2235{
2236 struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
2237 struct avs_shared_map_region_payload *mregions = NULL;
2238 struct audio_port_data *port = NULL;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002239 void *mmap_region_cmd = NULL;
2240 void *payload = NULL;
2241 struct asm_buffer_node *buffer_node = NULL;
2242 int rc = 0;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002243 int cmd_size = 0;
2244
2245 if (!ac || ac->apr == NULL || ac->mmap_apr == NULL) {
2246 pr_err("APR handle NULL\n");
2247 return -EINVAL;
2248 }
2249 pr_debug("%s: Session[%d]\n", __func__, ac->session);
2250
2251 buffer_node = kmalloc(sizeof(struct asm_buffer_node), GFP_KERNEL);
2252 if (!buffer_node)
2253 return -ENOMEM;
2254 cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
2255 + sizeof(struct avs_shared_map_region_payload) * bufcnt;
2256
2257 mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
2258 if (mmap_region_cmd == NULL) {
2259 pr_err("%s: Mem alloc failed\n", __func__);
2260 rc = -EINVAL;
2261 return rc;
2262 }
2263 mmap_regions = (struct avs_cmd_shared_mem_map_regions *)
2264 mmap_region_cmd;
2265 q6asm_add_mmaphdr(ac, &mmap_regions->hdr, cmd_size,
2266 TRUE, ((ac->session << 8) | dir));
2267 mmap_regions->hdr.opcode = ASM_CMD_SHARED_MEM_MAP_REGIONS;
2268 mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_EBI_POOL;
2269 mmap_regions->num_regions = bufcnt & 0x00ff;
2270 mmap_regions->property_flag = 0x00;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002271 payload = ((u8 *) mmap_region_cmd +
2272 sizeof(struct avs_cmd_shared_mem_map_regions));
2273 mregions = (struct avs_shared_map_region_payload *)payload;
2274
2275 ac->port[dir].tmp_hdl = 0;
2276 port = &ac->port[dir];
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002277 pr_debug("%s, buf_add 0x%x, bufsz: %d\n", __func__, buf_add, bufsz);
2278 mregions->shm_addr_lsw = buf_add;
2279 /* Using only 32 bit address */
2280 mregions->shm_addr_msw = 0;
2281 mregions->mem_size_bytes = bufsz;
2282 ++mregions;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002283
2284 rc = apr_send_pkt(ac->mmap_apr, (uint32_t *) mmap_region_cmd);
2285 if (rc < 0) {
2286 pr_err("mmap op[0x%x]rc[%d]\n",
2287 mmap_regions->hdr.opcode, rc);
2288 rc = -EINVAL;
2289 goto fail_cmd;
2290 }
2291
2292 rc = wait_event_timeout(ac->cmd_wait,
2293 (atomic_read(&ac->cmd_state) == 0 &&
2294 ac->port[dir].tmp_hdl), 5*HZ);
2295 if (!rc) {
2296 pr_err("timeout. waited for memory_map\n");
2297 rc = -EINVAL;
2298 goto fail_cmd;
2299 }
2300 buffer_node->buf_addr_lsw = buf_add;
2301 buffer_node->mmap_hdl = ac->port[dir].tmp_hdl;
2302 list_add_tail(&buffer_node->list, &ac->port[dir].mem_map_handle);
2303 ac->port[dir].tmp_hdl = 0;
2304 rc = 0;
2305
2306fail_cmd:
2307 kfree(mmap_region_cmd);
2308 return rc;
2309}
2310
2311int q6asm_memory_unmap(struct audio_client *ac, uint32_t buf_add, int dir)
2312{
2313 struct avs_cmd_shared_mem_unmap_regions mem_unmap;
2314 struct asm_buffer_node *buf_node = NULL;
2315 struct list_head *ptr, *next;
2316
2317 int rc = 0;
2318
2319 if (!ac || ac->apr == NULL || this_mmap.apr == NULL) {
2320 pr_err("APR handle NULL\n");
2321 return -EINVAL;
2322 }
2323 pr_debug("%s: Session[%d]\n", __func__, ac->session);
2324
2325 q6asm_add_mmaphdr(ac, &mem_unmap.hdr,
2326 sizeof(struct avs_cmd_shared_mem_unmap_regions),
2327 TRUE, ((ac->session << 8) | dir));
2328
2329 mem_unmap.hdr.opcode = ASM_CMD_SHARED_MEM_UNMAP_REGIONS;
2330 list_for_each_safe(ptr, next, &ac->port[dir].mem_map_handle) {
2331 buf_node = list_entry(ptr, struct asm_buffer_node,
2332 list);
2333 if (buf_node->buf_addr_lsw == buf_add) {
2334 pr_info("%s: Found the element\n", __func__);
2335 mem_unmap.mem_map_handle = buf_node->mmap_hdl;
2336 break;
2337 }
2338 }
2339 pr_debug("%s: mem_unmap-mem_map_handle: 0x%x",
2340 __func__, mem_unmap.mem_map_handle);
2341 rc = apr_send_pkt(ac->mmap_apr, (uint32_t *) &mem_unmap);
2342 if (rc < 0) {
2343 pr_err("mem_unmap op[0x%x]rc[%d]\n",
2344 mem_unmap.hdr.opcode, rc);
2345 rc = -EINVAL;
2346 goto fail_cmd;
2347 }
2348
2349 rc = wait_event_timeout(ac->cmd_wait,
2350 (atomic_read(&ac->cmd_state) == 0), 5 * HZ);
2351 if (!rc) {
2352 pr_err("timeout. waited for memory_map\n");
2353 rc = -EINVAL;
2354 goto fail_cmd;
2355 }
2356 list_for_each_safe(ptr, next, &ac->port[dir].mem_map_handle) {
2357 buf_node = list_entry(ptr, struct asm_buffer_node,
2358 list);
2359 if (buf_node->buf_addr_lsw == buf_add) {
2360 list_del(&buf_node->list);
2361 kfree(buf_node);
2362 }
2363 }
2364
2365 rc = 0;
2366fail_cmd:
2367 return rc;
2368}
2369
2370
2371static int q6asm_memory_map_regions(struct audio_client *ac, int dir,
2372 uint32_t bufsz, uint32_t bufcnt)
2373{
2374 struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
2375 struct avs_shared_map_region_payload *mregions = NULL;
2376 struct audio_port_data *port = NULL;
2377 struct audio_buffer *ab = NULL;
2378 void *mmap_region_cmd = NULL;
2379 void *payload = NULL;
2380 struct asm_buffer_node *buffer_node = NULL;
2381 int rc = 0;
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002382 int i = 0;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002383 int cmd_size = 0;
2384
2385 if (!ac || ac->apr == NULL || ac->mmap_apr == NULL) {
2386 pr_err("APR handle NULL\n");
2387 return -EINVAL;
2388 }
2389 pr_debug("%s: Session[%d]\n", __func__, ac->session);
2390
2391 cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
2392 + (sizeof(struct avs_shared_map_region_payload));
2393
2394 buffer_node = kzalloc(sizeof(struct asm_buffer_node) * bufcnt,
2395 GFP_KERNEL);
2396
2397 mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
2398 if ((mmap_region_cmd == NULL) || (buffer_node == NULL)) {
2399 pr_err("%s: Mem alloc failed\n", __func__);
2400 rc = -EINVAL;
2401 return rc;
2402 }
2403 mmap_regions = (struct avs_cmd_shared_mem_map_regions *)
2404 mmap_region_cmd;
2405 q6asm_add_mmaphdr(ac, &mmap_regions->hdr, cmd_size, TRUE,
2406 ((ac->session << 8) | dir));
2407 pr_debug("mmap_region=0x%p token=0x%x\n",
2408 mmap_regions, ((ac->session << 8) | dir));
2409
2410 mmap_regions->hdr.opcode = ASM_CMD_SHARED_MEM_MAP_REGIONS;
2411 mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_EBI_POOL;
2412 mmap_regions->num_regions = 1; /*bufcnt & 0x00ff; */
2413 mmap_regions->property_flag = 0x00;
2414 pr_debug("map_regions->nregions = %d\n", mmap_regions->num_regions);
2415 payload = ((u8 *) mmap_region_cmd +
2416 sizeof(struct avs_cmd_shared_mem_map_regions));
2417 mregions = (struct avs_shared_map_region_payload *)payload;
2418
2419 ac->port[dir].tmp_hdl = 0;
2420 port = &ac->port[dir];
2421 ab = &port->buf[0];
2422 mregions->shm_addr_lsw = ab->phys;
2423 /* Using only 32 bit address */
2424 mregions->shm_addr_msw = 0;
2425 mregions->mem_size_bytes = (bufsz * bufcnt);
2426
2427 rc = apr_send_pkt(ac->mmap_apr, (uint32_t *) mmap_region_cmd);
2428 if (rc < 0) {
2429 pr_err("mmap_regions op[0x%x]rc[%d]\n",
2430 mmap_regions->hdr.opcode, rc);
2431 rc = -EINVAL;
2432 goto fail_cmd;
2433 }
2434
2435 rc = wait_event_timeout(ac->cmd_wait,
2436 (atomic_read(&ac->cmd_state) == 0)
2437 , 5*HZ);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002438 if (!rc) {
2439 pr_err("timeout. waited for memory_map\n");
2440 rc = -EINVAL;
2441 goto fail_cmd;
2442 }
2443 mutex_lock(&ac->cmd_lock);
2444
2445 for (i = 0; i < bufcnt; i++) {
2446 ab = &port->buf[i];
2447 buffer_node[i].buf_addr_lsw = ab->phys;
2448 buffer_node[i].mmap_hdl = ac->port[dir].tmp_hdl;
2449 list_add_tail(&buffer_node[i].list,
2450 &ac->port[dir].mem_map_handle);
2451 pr_debug("%s: i=%d, bufadd[i] = 0x%x, maphdl[i] = 0x%x\n",
2452 __func__, i, buffer_node[i].buf_addr_lsw,
2453 buffer_node[i].mmap_hdl);
2454 }
2455 ac->port[dir].tmp_hdl = 0;
2456 mutex_unlock(&ac->cmd_lock);
2457 rc = 0;
2458 pr_debug("%s: exit\n", __func__);
2459fail_cmd:
2460 kfree(mmap_region_cmd);
2461 return rc;
2462}
2463
2464static int q6asm_memory_unmap_regions(struct audio_client *ac, int dir,
2465 uint32_t bufsz, uint32_t bufcnt)
2466{
2467 struct avs_cmd_shared_mem_unmap_regions mem_unmap;
2468 struct audio_port_data *port = NULL;
2469 struct asm_buffer_node *buf_node = NULL;
2470 struct list_head *ptr, *next;
2471 uint32_t buf_add;
2472 int rc = 0;
2473 int cmd_size = 0;
2474
2475 if (!ac || ac->apr == NULL || ac->mmap_apr == NULL) {
2476 pr_err("APR handle NULL\n");
2477 return -EINVAL;
2478 }
2479 pr_debug("%s: Session[%d]\n", __func__, ac->session);
2480
2481 cmd_size = sizeof(struct avs_cmd_shared_mem_unmap_regions);
2482 q6asm_add_mmaphdr(ac, &mem_unmap.hdr, cmd_size,
2483 TRUE, ((ac->session << 8) | dir));
2484 port = &ac->port[dir];
2485 buf_add = (uint32_t)port->buf->phys;
2486 mem_unmap.hdr.opcode = ASM_CMD_SHARED_MEM_UNMAP_REGIONS;
2487 list_for_each_safe(ptr, next, &ac->port[dir].mem_map_handle) {
2488 buf_node = list_entry(ptr, struct asm_buffer_node,
2489 list);
2490 if (buf_node->buf_addr_lsw == buf_add) {
2491 pr_debug("%s: Found the element\n", __func__);
2492 mem_unmap.mem_map_handle = buf_node->mmap_hdl;
2493 break;
2494 }
2495 }
2496
2497 pr_debug("%s: mem_unmap-mem_map_handle: 0x%x",
2498 __func__, mem_unmap.mem_map_handle);
2499 rc = apr_send_pkt(ac->mmap_apr, (uint32_t *) &mem_unmap);
2500 if (rc < 0) {
2501 pr_err("mmap_regions op[0x%x]rc[%d]\n",
2502 mem_unmap.hdr.opcode, rc);
2503 goto fail_cmd;
2504 }
2505
2506 rc = wait_event_timeout(ac->cmd_wait,
2507 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2508 if (!rc) {
2509 pr_err("timeout. waited for memory_unmap\n");
2510 goto fail_cmd;
2511 }
2512 list_for_each_safe(ptr, next, &ac->port[dir].mem_map_handle) {
2513 buf_node = list_entry(ptr, struct asm_buffer_node,
2514 list);
2515 if (buf_node->buf_addr_lsw == buf_add) {
2516 list_del(&buf_node->list);
2517 kfree(buf_node);
2518 }
2519 }
2520 rc = 0;
2521
2522fail_cmd:
2523 return rc;
2524}
2525
2526int q6asm_set_lrgain(struct audio_client *ac, int left_gain, int right_gain)
2527{
2528 struct asm_volume_ctrl_lr_chan_gain lrgain;
2529 int sz = 0;
2530 int rc = 0;
2531
2532 sz = sizeof(struct asm_volume_ctrl_lr_chan_gain);
2533 q6asm_add_hdr_async(ac, &lrgain.hdr, sz, TRUE);
2534 lrgain.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
2535 lrgain.param.data_payload_addr_lsw = 0;
2536 lrgain.param.data_payload_addr_msw = 0;
2537 lrgain.param.mem_map_handle = 0;
2538 lrgain.param.data_payload_size = sizeof(lrgain) -
2539 sizeof(lrgain.hdr) - sizeof(lrgain.param);
2540 lrgain.data.module_id = ASM_MODULE_ID_VOL_CTRL;
2541 lrgain.data.param_id = ASM_PARAM_ID_VOL_CTRL_LR_CHANNEL_GAIN;
2542 lrgain.data.param_size = lrgain.param.data_payload_size -
2543 sizeof(lrgain.data);
2544 lrgain.data.reserved = 0;
2545 lrgain.l_chan_gain = left_gain;
2546 lrgain.r_chan_gain = right_gain;
2547 rc = apr_send_pkt(ac->apr, (uint32_t *) &lrgain);
2548 if (rc < 0) {
2549 pr_err("%s: set-params send failed paramid[0x%x]\n", __func__,
2550 lrgain.data.param_id);
2551 rc = -EINVAL;
2552 goto fail_cmd;
2553 }
2554
2555 rc = wait_event_timeout(ac->cmd_wait,
2556 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2557 if (!rc) {
2558 pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
2559 lrgain.data.param_id);
2560 rc = -EINVAL;
2561 goto fail_cmd;
2562 }
2563 rc = 0;
2564fail_cmd:
2565 return rc;
2566}
2567
2568int q6asm_set_mute(struct audio_client *ac, int muteflag)
2569{
2570 struct asm_volume_ctrl_mute_config mute;
2571 int sz = 0;
2572 int rc = 0;
2573
2574 sz = sizeof(struct asm_volume_ctrl_mute_config);
2575 q6asm_add_hdr_async(ac, &mute.hdr, sz, TRUE);
2576 mute.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
2577 mute.param.data_payload_addr_lsw = 0;
2578 mute.param.data_payload_addr_msw = 0;
2579 mute.param.mem_map_handle = 0;
2580 mute.param.data_payload_size = sizeof(mute) -
2581 sizeof(mute.hdr) - sizeof(mute.param);
2582 mute.data.module_id = ASM_MODULE_ID_VOL_CTRL;
2583 mute.data.param_id = ASM_PARAM_ID_VOL_CTRL_MUTE_CONFIG;
2584 mute.data.param_size = mute.param.data_payload_size - sizeof(mute.data);
2585 mute.data.reserved = 0;
2586 mute.mute_flag = muteflag;
2587
2588 rc = apr_send_pkt(ac->apr, (uint32_t *) &mute);
2589 if (rc < 0) {
2590 pr_err("%s: set-params send failed paramid[0x%x]\n", __func__,
2591 mute.data.param_id);
2592 rc = -EINVAL;
2593 goto fail_cmd;
2594 }
2595
2596 rc = wait_event_timeout(ac->cmd_wait,
2597 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2598 if (!rc) {
2599 pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
2600 mute.data.param_id);
2601 rc = -EINVAL;
2602 goto fail_cmd;
2603 }
2604 rc = 0;
2605fail_cmd:
2606 return rc;
2607}
2608
2609int q6asm_set_volume(struct audio_client *ac, int volume)
2610{
2611 struct asm_volume_ctrl_master_gain vol;
2612 int sz = 0;
2613 int rc = 0;
2614
2615 sz = sizeof(struct asm_volume_ctrl_master_gain);
2616 q6asm_add_hdr_async(ac, &vol.hdr, sz, TRUE);
2617 vol.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
2618 vol.param.data_payload_addr_lsw = 0;
2619 vol.param.data_payload_addr_msw = 0;
2620
2621
2622 vol.param.mem_map_handle = 0;
2623 vol.param.data_payload_size = sizeof(vol) -
2624 sizeof(vol.hdr) - sizeof(vol.param);
2625 vol.data.module_id = ASM_MODULE_ID_VOL_CTRL;
2626 vol.data.param_id = ASM_PARAM_ID_VOL_CTRL_MASTER_GAIN;
2627 vol.data.param_size = vol.param.data_payload_size - sizeof(vol.data);
2628 vol.data.reserved = 0;
2629 vol.master_gain = volume;
2630
2631 rc = apr_send_pkt(ac->apr, (uint32_t *) &vol);
2632 if (rc < 0) {
2633 pr_err("%s: set-params send failed paramid[0x%x]\n", __func__,
2634 vol.data.param_id);
2635 rc = -EINVAL;
2636 goto fail_cmd;
2637 }
2638
2639 rc = wait_event_timeout(ac->cmd_wait,
2640 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2641 if (!rc) {
2642 pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
2643 vol.data.param_id);
2644 rc = -EINVAL;
2645 goto fail_cmd;
2646 }
2647 rc = 0;
2648fail_cmd:
2649 return rc;
2650}
2651int q6asm_set_softpause(struct audio_client *ac,
2652 struct asm_softpause_params *pause_param)
2653{
2654 struct asm_soft_pause_params softpause;
2655 int sz = 0;
2656 int rc = 0;
2657
2658 sz = sizeof(struct asm_soft_pause_params);
2659 q6asm_add_hdr_async(ac, &softpause.hdr, sz, TRUE);
2660 softpause.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
2661
2662 softpause.param.data_payload_addr_lsw = 0;
2663 softpause.param.data_payload_addr_msw = 0;
2664 softpause.param.mem_map_handle = 0;
2665 softpause.param.data_payload_size = sizeof(softpause) -
2666 sizeof(softpause.hdr) - sizeof(softpause.param);
2667 softpause.data.module_id = ASM_MODULE_ID_VOL_CTRL;
2668 softpause.data.param_id = ASM_PARAM_ID_SOFT_PAUSE_PARAMETERS;
2669 softpause.data.param_size = softpause.param.data_payload_size -
2670 sizeof(softpause.data);
2671 softpause.data.reserved = 0;
2672 softpause.enable_flag = pause_param->enable;
2673 softpause.period = pause_param->period;
2674 softpause.step = pause_param->step;
2675 softpause.ramping_curve = pause_param->rampingcurve;
2676
2677 rc = apr_send_pkt(ac->apr, (uint32_t *) &softpause);
2678 if (rc < 0) {
2679 pr_err("%s: set-params send failed paramid[0x%x]\n", __func__,
2680 softpause.data.param_id);
2681 rc = -EINVAL;
2682 goto fail_cmd;
2683 }
2684
2685 rc = wait_event_timeout(ac->cmd_wait,
2686 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2687 if (!rc) {
2688 pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
2689 softpause.data.param_id);
2690 rc = -EINVAL;
2691 goto fail_cmd;
2692 }
2693 rc = 0;
2694fail_cmd:
2695 return rc;
2696}
2697
2698int q6asm_set_softvolume(struct audio_client *ac,
2699 struct asm_softvolume_params *softvol_param)
2700{
2701 struct asm_soft_step_volume_params softvol;
2702 int sz = 0;
2703 int rc = 0;
2704
2705 sz = sizeof(struct asm_soft_step_volume_params);
2706 q6asm_add_hdr_async(ac, &softvol.hdr, sz, TRUE);
2707 softvol.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
2708 softvol.param.data_payload_addr_lsw = 0;
2709 softvol.param.data_payload_addr_msw = 0;
2710 softvol.param.mem_map_handle = 0;
2711 softvol.param.data_payload_size = sizeof(softvol) -
2712 sizeof(softvol.hdr) - sizeof(softvol.param);
2713 softvol.data.module_id = ASM_MODULE_ID_VOL_CTRL;
2714 softvol.data.param_id = ASM_PARAM_ID_SOFT_VOL_STEPPING_PARAMETERS;
2715 softvol.data.param_size = softvol.param.data_payload_size -
2716 sizeof(softvol.data);
2717 softvol.data.reserved = 0;
2718 softvol.period = softvol_param->period;
2719 softvol.step = softvol_param->step;
2720 softvol.ramping_curve = softvol_param->rampingcurve;
2721
2722 rc = apr_send_pkt(ac->apr, (uint32_t *) &softvol);
2723 if (rc < 0) {
2724 pr_err("%s: set-params send failed paramid[0x%x]\n", __func__,
2725 softvol.data.param_id);
2726 rc = -EINVAL;
2727 goto fail_cmd;
2728 }
2729
2730 rc = wait_event_timeout(ac->cmd_wait,
2731 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2732 if (!rc) {
2733 pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
2734 softvol.data.param_id);
2735 rc = -EINVAL;
2736 goto fail_cmd;
2737 }
2738 rc = 0;
2739fail_cmd:
2740 return rc;
2741}
2742
2743int q6asm_equalizer(struct audio_client *ac, void *eq_p)
2744{
2745 struct asm_eq_params eq;
2746 struct msm_audio_eq_stream_config *eq_params = NULL;
2747 int i = 0;
2748 int sz = 0;
2749 int rc = 0;
2750
2751 if (eq_p == NULL) {
2752 pr_err("%s[%d]: Invalid Eq param\n", __func__, ac->session);
2753 rc = -EINVAL;
2754 goto fail_cmd;
2755 }
2756 sz = sizeof(struct asm_eq_params);
2757 eq_params = (struct msm_audio_eq_stream_config *) eq_p;
2758 q6asm_add_hdr(ac, &eq.hdr, sz, TRUE);
2759
2760 eq.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
2761 eq.param.data_payload_addr_lsw = 0;
2762 eq.param.data_payload_addr_msw = 0;
2763 eq.param.mem_map_handle = 0;
2764 eq.param.data_payload_size = sizeof(eq) -
2765 sizeof(eq.hdr) - sizeof(eq.param);
2766 eq.data.module_id = ASM_MODULE_ID_EQUALIZER;
2767 eq.data.param_id = ASM_PARAM_ID_EQUALIZER_PARAMETERS;
2768 eq.data.param_size = eq.param.data_payload_size - sizeof(eq.data);
2769 eq.enable_flag = eq_params->enable;
2770 eq.num_bands = eq_params->num_bands;
2771
2772 pr_debug("%s: enable:%d numbands:%d\n", __func__, eq_params->enable,
2773 eq_params->num_bands);
2774 for (i = 0; i < eq_params->num_bands; i++) {
2775 eq.eq_bands[i].band_idx =
2776 eq_params->eq_bands[i].band_idx;
2777 eq.eq_bands[i].filterype =
2778 eq_params->eq_bands[i].filter_type;
2779 eq.eq_bands[i].center_freq_hz =
2780 eq_params->eq_bands[i].center_freq_hz;
2781 eq.eq_bands[i].filter_gain =
2782 eq_params->eq_bands[i].filter_gain;
2783 eq.eq_bands[i].q_factor =
2784 eq_params->eq_bands[i].q_factor;
2785 pr_debug("%s: filter_type:%u bandnum:%d\n", __func__,
2786 eq_params->eq_bands[i].filter_type, i);
2787 pr_debug("%s: center_freq_hz:%u bandnum:%d\n", __func__,
2788 eq_params->eq_bands[i].center_freq_hz, i);
2789 pr_debug("%s: filter_gain:%d bandnum:%d\n", __func__,
2790 eq_params->eq_bands[i].filter_gain, i);
2791 pr_debug("%s: q_factor:%d bandnum:%d\n", __func__,
2792 eq_params->eq_bands[i].q_factor, i);
2793 }
2794 rc = apr_send_pkt(ac->apr, (uint32_t *)&eq);
2795 if (rc < 0) {
2796 pr_err("%s: set-params send failed paramid[0x%x]\n", __func__,
2797 eq.data.param_id);
2798 rc = -EINVAL;
2799 goto fail_cmd;
2800 }
2801
2802 rc = wait_event_timeout(ac->cmd_wait,
2803 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2804 if (!rc) {
2805 pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
2806 eq.data.param_id);
2807 rc = -EINVAL;
2808 goto fail_cmd;
2809 }
2810 rc = 0;
2811fail_cmd:
2812 return rc;
2813}
2814
2815int q6asm_read(struct audio_client *ac)
2816{
2817 struct asm_data_cmd_read_v2 read;
2818 struct asm_buffer_node *buf_node = NULL;
2819 struct list_head *ptr, *next;
2820 struct audio_buffer *ab;
2821 int dsp_buf;
2822 struct audio_port_data *port;
2823 int rc;
2824 if (!ac || ac->apr == NULL) {
2825 pr_err("APR handle NULL\n");
2826 return -EINVAL;
2827 }
2828 if (ac->io_mode == SYNC_IO_MODE) {
2829 port = &ac->port[OUT];
2830
2831 q6asm_add_hdr(ac, &read.hdr, sizeof(read), FALSE);
2832
2833 mutex_lock(&port->lock);
2834
2835 dsp_buf = port->dsp_buf;
2836 ab = &port->buf[dsp_buf];
2837
2838 pr_debug("%s:session[%d]dsp-buf[%d][%p]cpu_buf[%d][%p]\n",
2839 __func__,
2840 ac->session,
2841 dsp_buf,
2842 (void *)port->buf[dsp_buf].data,
2843 port->cpu_buf,
2844 (void *)port->buf[port->cpu_buf].phys);
2845
2846 read.hdr.opcode = ASM_DATA_CMD_READ_V2;
2847 read.buf_addr_lsw = ab->phys;
2848 read.buf_addr_msw = 0;
2849
2850 list_for_each_safe(ptr, next, &ac->port[OUT].mem_map_handle) {
2851 buf_node = list_entry(ptr, struct asm_buffer_node,
2852 list);
2853 if (buf_node->buf_addr_lsw == (uint32_t) ab->phys)
2854 read.mem_map_handle = buf_node->mmap_hdl;
2855 }
2856 pr_debug("memory_map handle in q6asm_read: [%0x]:",
2857 read.mem_map_handle);
2858 read.buf_size = ab->size;
2859 read.seq_id = port->dsp_buf;
2860 read.hdr.token = port->dsp_buf;
2861 port->dsp_buf = (port->dsp_buf + 1) & (port->max_buf_cnt - 1);
2862 mutex_unlock(&port->lock);
2863 pr_debug("%s:buf add[0x%x] token[%d] uid[%d]\n", __func__,
2864 read.buf_addr_lsw,
2865 read.hdr.token,
2866 read.seq_id);
2867 rc = apr_send_pkt(ac->apr, (uint32_t *) &read);
2868 if (rc < 0) {
2869 pr_err("read op[0x%x]rc[%d]\n", read.hdr.opcode, rc);
2870 goto fail_cmd;
2871 }
2872 return 0;
2873 }
2874fail_cmd:
2875 return -EINVAL;
2876}
2877
2878int q6asm_read_nolock(struct audio_client *ac)
2879{
2880 struct asm_data_cmd_read_v2 read;
2881 struct asm_buffer_node *buf_node = NULL;
2882 struct list_head *ptr, *next;
2883 struct audio_buffer *ab;
2884 int dsp_buf;
2885 struct audio_port_data *port;
2886 int rc;
2887 if (!ac || ac->apr == NULL) {
2888 pr_err("APR handle NULL\n");
2889 return -EINVAL;
2890 }
2891 if (ac->io_mode == SYNC_IO_MODE) {
2892 port = &ac->port[OUT];
2893
2894 q6asm_add_hdr_async(ac, &read.hdr, sizeof(read), FALSE);
2895
2896
2897 dsp_buf = port->dsp_buf;
2898 ab = &port->buf[dsp_buf];
2899
2900 pr_debug("%s:session[%d]dsp-buf[%d][%p]cpu_buf[%d][%p]\n",
2901 __func__,
2902 ac->session,
2903 dsp_buf,
2904 (void *)port->buf[dsp_buf].data,
2905 port->cpu_buf,
2906 (void *)port->buf[port->cpu_buf].phys);
2907
2908 read.hdr.opcode = ASM_DATA_CMD_READ_V2;
2909 read.buf_addr_lsw = ab->phys;
2910 read.buf_addr_msw = 0;
2911 read.buf_size = ab->size;
2912 read.seq_id = port->dsp_buf;
2913 read.hdr.token = port->dsp_buf;
2914
2915 list_for_each_safe(ptr, next, &ac->port[OUT].mem_map_handle) {
2916 buf_node = list_entry(ptr, struct asm_buffer_node,
2917 list);
2918 if (buf_node->buf_addr_lsw == (uint32_t)ab->phys) {
2919 read.mem_map_handle = buf_node->mmap_hdl;
2920 break;
2921 }
2922 }
2923
2924 port->dsp_buf = (port->dsp_buf + 1) & (port->max_buf_cnt - 1);
2925 pr_debug("%s:buf add[0x%x] token[%d] uid[%d]\n", __func__,
2926 read.buf_addr_lsw,
2927 read.hdr.token,
2928 read.seq_id);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002929 rc = apr_send_pkt(ac->apr, (uint32_t *) &read);
2930 if (rc < 0) {
2931 pr_err("read op[0x%x]rc[%d]\n", read.hdr.opcode, rc);
2932 goto fail_cmd;
2933 }
2934 return 0;
2935 }
2936fail_cmd:
2937 return -EINVAL;
2938}
2939
2940int q6asm_async_write(struct audio_client *ac,
2941 struct audio_aio_write_param *param)
2942{
2943 int rc = 0;
2944 struct asm_data_cmd_write_v2 write;
2945 struct asm_buffer_node *buf_node = NULL;
2946 struct list_head *ptr, *next;
2947 struct audio_buffer *ab;
2948 struct audio_port_data *port;
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002949 u32 lbuf_addr_lsw;
2950 u32 liomode;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002951
2952 if (!ac || ac->apr == NULL) {
2953 pr_err("%s: APR handle NULL\n", __func__);
2954 return -EINVAL;
2955 }
2956
2957 q6asm_add_hdr_async(ac, &write.hdr, sizeof(write), FALSE);
2958
2959 port = &ac->port[IN];
2960 ab = &port->buf[port->dsp_buf];
2961
2962 /* Pass physical address as token for AIO scheme */
2963 write.hdr.token = param->uid;
2964 write.hdr.opcode = ASM_DATA_CMD_WRITE_V2;
2965 write.buf_addr_lsw = param->paddr;
2966 write.buf_addr_msw = 0x00;
2967 write.buf_size = param->len;
2968 write.timestamp_msw = param->msw_ts;
2969 write.timestamp_lsw = param->lsw_ts;
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002970 liomode = (ASYNC_IO_MODE | NT_MODE);
2971
2972 if (ac->io_mode == liomode) {
2973 pr_info("%s: subtracting 32 for header\n", __func__);
2974 lbuf_addr_lsw = (write.buf_addr_lsw - 32);
2975 } else{
2976 lbuf_addr_lsw = write.buf_addr_lsw;
2977 }
2978
2979 pr_debug("%s: token[0x%x], buf_addr_lsw[0x%x], buf_size[0x%x], ts_msw[0x%x], ts_lsw[0x%x], lbuf_addr_lsw: 0x[%x]\n",
2980 __func__,
2981 write.hdr.token, write.buf_addr_lsw,
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002982 write.buf_size, write.timestamp_msw,
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002983 write.timestamp_lsw, lbuf_addr_lsw);
2984
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002985 /* Use 0xFF00 for disabling timestamps */
2986 if (param->flags == 0xFF00)
2987 write.flags = (0x00000000 | (param->flags & 0x800000FF));
2988 else
2989 write.flags = (0x80000000 | param->flags);
2990
2991 write.seq_id = param->uid;
2992 list_for_each_safe(ptr, next, &ac->port[IN].mem_map_handle) {
2993 buf_node = list_entry(ptr, struct asm_buffer_node,
2994 list);
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002995 if (buf_node->buf_addr_lsw == lbuf_addr_lsw) {
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002996 write.mem_map_handle = buf_node->mmap_hdl;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002997 break;
2998 }
2999 }
3000
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07003001 rc = apr_send_pkt(ac->apr, (uint32_t *) &write);
3002 if (rc < 0) {
3003 pr_debug("[%s] write op[0x%x]rc[%d]\n", __func__,
3004 write.hdr.opcode, rc);
3005 goto fail_cmd;
3006 }
3007 return 0;
3008fail_cmd:
3009 return -EINVAL;
3010}
3011
3012int q6asm_async_read(struct audio_client *ac,
3013 struct audio_aio_read_param *param)
3014{
3015 int rc = 0;
3016 struct asm_data_cmd_read_v2 read;
3017 struct asm_buffer_node *buf_node = NULL;
3018 struct list_head *ptr, *next;
Harmandeep Singheaf59b42012-06-05 21:46:02 -07003019 u32 lbuf_addr_lsw;
3020 u32 liomode;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07003021
3022 if (!ac || ac->apr == NULL) {
3023 pr_err("%s: APR handle NULL\n", __func__);
3024 return -EINVAL;
3025 }
3026
3027 q6asm_add_hdr_async(ac, &read.hdr, sizeof(read), FALSE);
3028
3029 /* Pass physical address as token for AIO scheme */
3030 read.hdr.token = param->paddr;
3031 read.hdr.opcode = ASM_DATA_CMD_READ_V2;
3032 read.buf_addr_lsw = param->paddr;
3033 read.buf_addr_msw = 0;
3034 read.buf_size = param->len;
3035 read.seq_id = param->uid;
Harmandeep Singheaf59b42012-06-05 21:46:02 -07003036 liomode = (NT_MODE | ASYNC_IO_MODE);
3037 if (ac->io_mode == liomode) {
3038 pr_info("%s: subtracting 32 for header\n", __func__);
3039 lbuf_addr_lsw = (read.buf_addr_lsw - 32);
3040 } else{
3041 lbuf_addr_lsw = read.buf_addr_lsw;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07003042 }
3043
Harmandeep Singheaf59b42012-06-05 21:46:02 -07003044 list_for_each_safe(ptr, next, &ac->port[IN].mem_map_handle) {
3045 buf_node = list_entry(ptr, struct asm_buffer_node, list);
3046 if (buf_node->buf_addr_lsw == lbuf_addr_lsw) {
3047 read.mem_map_handle = buf_node->mmap_hdl;
3048 break;
3049 }
3050 }
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07003051
3052 rc = apr_send_pkt(ac->apr, (uint32_t *) &read);
3053 if (rc < 0) {
3054 pr_debug("[%s] read op[0x%x]rc[%d]\n", __func__,
3055 read.hdr.opcode, rc);
3056 goto fail_cmd;
3057 }
3058 return 0;
3059fail_cmd:
3060 return -EINVAL;
3061}
3062
3063int q6asm_write(struct audio_client *ac, uint32_t len, uint32_t msw_ts,
3064 uint32_t lsw_ts, uint32_t flags)
3065{
3066 int rc = 0;
3067 struct asm_data_cmd_write_v2 write;
3068 struct asm_buffer_node *buf_node = NULL;
3069 struct audio_port_data *port;
3070 struct audio_buffer *ab;
3071 int dsp_buf = 0;
3072
3073 if (!ac || ac->apr == NULL) {
3074 pr_err("APR handle NULL\n");
3075 return -EINVAL;
3076 }
3077 pr_debug("%s: session[%d] len=%d", __func__, ac->session, len);
3078 if (ac->io_mode == SYNC_IO_MODE) {
3079 port = &ac->port[IN];
3080
3081 q6asm_add_hdr(ac, &write.hdr, sizeof(write),
3082 FALSE);
3083 mutex_lock(&port->lock);
3084
3085 dsp_buf = port->dsp_buf;
3086 ab = &port->buf[dsp_buf];
3087
3088 write.hdr.token = port->dsp_buf;
3089 write.hdr.opcode = ASM_DATA_CMD_WRITE_V2;
3090 write.buf_addr_lsw = ab->phys;
3091 write.buf_addr_msw = 0;
3092 write.buf_size = len;
3093 write.seq_id = port->dsp_buf;
3094 write.timestamp_lsw = lsw_ts;
3095 write.timestamp_msw = msw_ts;
3096 /* Use 0xFF00 for disabling timestamps */
3097 if (flags == 0xFF00)
3098 write.flags = (0x00000000 | (flags & 0x800000FF));
3099 else
3100 write.flags = (0x80000000 | flags);
3101 port->dsp_buf = (port->dsp_buf + 1) & (port->max_buf_cnt - 1);
3102 buf_node = list_first_entry(&ac->port[IN].mem_map_handle,
3103 struct asm_buffer_node,
3104 list);
3105 write.mem_map_handle = buf_node->mmap_hdl;
3106
Harmandeep Singheaf59b42012-06-05 21:46:02 -07003107 pr_debug("%s:ab->phys[0x%x]bufadd[0x%x] token[0x%x]buf_id[0x%x]buf_size[0x%x]mmaphdl[0x%x]"
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07003108 , __func__,
3109 ab->phys,
3110 write.buf_addr_lsw,
3111 write.hdr.token,
3112 write.seq_id,
3113 write.buf_size,
3114 write.mem_map_handle);
3115 mutex_unlock(&port->lock);
3116
3117 config_debug_fs_write(ab);
3118
3119 rc = apr_send_pkt(ac->apr, (uint32_t *) &write);
3120 if (rc < 0) {
3121 pr_err("write op[0x%x]rc[%d]\n", write.hdr.opcode, rc);
3122 goto fail_cmd;
3123 }
3124 pr_debug("%s: WRITE SUCCESS\n", __func__);
3125 return 0;
3126 }
3127fail_cmd:
3128 return -EINVAL;
3129}
3130
3131int q6asm_write_nolock(struct audio_client *ac, uint32_t len, uint32_t msw_ts,
3132 uint32_t lsw_ts, uint32_t flags)
3133{
3134 int rc = 0;
3135 struct asm_data_cmd_write_v2 write;
3136 struct asm_buffer_node *buf_node = NULL;
3137 struct audio_port_data *port;
3138 struct audio_buffer *ab;
3139 int dsp_buf = 0;
3140
3141 if (!ac || ac->apr == NULL) {
3142 pr_err("APR handle NULL\n");
3143 return -EINVAL;
3144 }
3145 pr_debug("%s: session[%d] len=%d", __func__, ac->session, len);
3146 if (ac->io_mode == SYNC_IO_MODE) {
3147 port = &ac->port[IN];
3148
3149 q6asm_add_hdr_async(ac, &write.hdr, sizeof(write),
3150 FALSE);
3151
3152 dsp_buf = port->dsp_buf;
3153 ab = &port->buf[dsp_buf];
3154
3155 write.hdr.token = port->dsp_buf;
3156 write.hdr.opcode = ASM_DATA_CMD_WRITE_V2;
3157 write.buf_addr_lsw = ab->phys;
3158 write.buf_addr_msw = 0;
3159 write.buf_size = len;
3160 write.seq_id = port->dsp_buf;
3161 write.timestamp_lsw = lsw_ts;
3162 write.timestamp_msw = msw_ts;
3163 buf_node = list_first_entry(&ac->port[IN].mem_map_handle,
3164 struct asm_buffer_node,
3165 list);
3166 write.mem_map_handle = buf_node->mmap_hdl;
3167 /* Use 0xFF00 for disabling timestamps */
3168 if (flags == 0xFF00)
3169 write.flags = (0x00000000 | (flags & 0x800000FF));
3170 else
3171 write.flags = (0x80000000 | flags);
3172 port->dsp_buf = (port->dsp_buf + 1) & (port->max_buf_cnt - 1);
3173
Harmandeep Singheaf59b42012-06-05 21:46:02 -07003174 pr_debug("%s:ab->phys[0x%x]bufadd[0x%x]token[0x%x] buf_id[0x%x]buf_size[0x%x]mmaphdl[0x%x]"
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07003175 , __func__,
3176 ab->phys,
3177 write.buf_addr_lsw,
3178 write.hdr.token,
3179 write.seq_id,
3180 write.buf_size,
3181 write.mem_map_handle);
3182
3183 rc = apr_send_pkt(ac->apr, (uint32_t *) &write);
3184 if (rc < 0) {
3185 pr_err("write op[0x%x]rc[%d]\n", write.hdr.opcode, rc);
3186 goto fail_cmd;
3187 }
3188 pr_debug("%s: WRITE SUCCESS\n", __func__);
3189 return 0;
3190 }
3191fail_cmd:
3192 return -EINVAL;
3193}
3194
3195uint64_t q6asm_get_session_time(struct audio_client *ac)
3196{
3197 struct apr_hdr hdr;
3198 int rc;
3199
3200 if (!ac || ac->apr == NULL) {
3201 pr_err("APR handle NULL\n");
3202 return -EINVAL;
3203 }
3204 q6asm_add_hdr(ac, &hdr, sizeof(hdr), TRUE);
3205 hdr.opcode = ASM_SESSION_CMD_GET_SESSIONTIME_V3;
3206 atomic_set(&ac->cmd_state, 1);
3207
3208 pr_debug("%s: session[%d]opcode[0x%x]\n", __func__,
3209 ac->session,
3210 hdr.opcode);
3211 rc = apr_send_pkt(ac->apr, (uint32_t *) &hdr);
3212 if (rc < 0) {
3213 pr_err("Commmand 0x%x failed\n", hdr.opcode);
3214 goto fail_cmd;
3215 }
3216 rc = wait_event_timeout(ac->cmd_wait,
3217 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
3218 if (!rc) {
3219 pr_err("%s: timeout in getting session time from DSP\n",
3220 __func__);
3221 goto fail_cmd;
3222 }
3223 return ac->time_stamp;
3224
3225fail_cmd:
3226 return -EINVAL;
3227}
3228
3229int q6asm_cmd(struct audio_client *ac, int cmd)
3230{
3231 struct apr_hdr hdr;
3232 int rc;
3233 atomic_t *state;
3234 int cnt = 0;
3235
3236 if (!ac || ac->apr == NULL) {
3237 pr_err("APR handle NULL\n");
3238 return -EINVAL;
3239 }
3240 q6asm_add_hdr(ac, &hdr, sizeof(hdr), TRUE);
3241 switch (cmd) {
3242 case CMD_PAUSE:
3243 pr_debug("%s:CMD_PAUSE\n", __func__);
3244 hdr.opcode = ASM_SESSION_CMD_PAUSE;
3245 state = &ac->cmd_state;
3246 break;
3247 case CMD_FLUSH:
3248 pr_debug("%s:CMD_FLUSH\n", __func__);
3249 hdr.opcode = ASM_STREAM_CMD_FLUSH;
3250 state = &ac->cmd_state;
3251 break;
3252 case CMD_OUT_FLUSH:
3253 pr_debug("%s:CMD_OUT_FLUSH\n", __func__);
3254 hdr.opcode = ASM_STREAM_CMD_FLUSH_READBUFS;
3255 state = &ac->cmd_state;
3256 break;
3257 case CMD_EOS:
3258 pr_debug("%s:CMD_EOS\n", __func__);
3259 hdr.opcode = ASM_DATA_CMD_EOS;
3260 atomic_set(&ac->cmd_state, 0);
3261 state = &ac->cmd_state;
3262 break;
3263 case CMD_CLOSE:
3264 pr_debug("%s:CMD_CLOSE\n", __func__);
3265 hdr.opcode = ASM_STREAM_CMD_CLOSE;
3266 state = &ac->cmd_state;
3267 break;
3268 default:
3269 pr_err("Invalid format[%d]\n", cmd);
3270 goto fail_cmd;
3271 }
3272 pr_debug("%s:session[%d]opcode[0x%x] ", __func__,
3273 ac->session,
3274 hdr.opcode);
3275 rc = apr_send_pkt(ac->apr, (uint32_t *) &hdr);
3276 if (rc < 0) {
3277 pr_err("Commmand 0x%x failed\n", hdr.opcode);
3278 goto fail_cmd;
3279 }
3280 rc = wait_event_timeout(ac->cmd_wait, (atomic_read(state) == 0), 5*HZ);
3281 if (!rc) {
3282 pr_err("timeout. waited for response opcode[0x%x]\n",
3283 hdr.opcode);
3284 goto fail_cmd;
3285 }
3286 if (cmd == CMD_FLUSH)
3287 q6asm_reset_buf_state(ac);
3288 if (cmd == CMD_CLOSE) {
3289 /* check if DSP return all buffers */
3290 if (ac->port[IN].buf) {
3291 for (cnt = 0; cnt < ac->port[IN].max_buf_cnt;
3292 cnt++) {
3293 if (ac->port[IN].buf[cnt].used == IN) {
3294 pr_debug("Write Buf[%d] not returned\n",
3295 cnt);
3296 }
3297 }
3298 }
3299 if (ac->port[OUT].buf) {
3300 for (cnt = 0; cnt < ac->port[OUT].max_buf_cnt; cnt++) {
3301 if (ac->port[OUT].buf[cnt].used == OUT) {
3302 pr_debug("Read Buf[%d] not returned\n",
3303 cnt);
3304 }
3305 }
3306 }
3307 }
3308 return 0;
3309fail_cmd:
3310 return -EINVAL;
3311}
3312
3313int q6asm_cmd_nowait(struct audio_client *ac, int cmd)
3314{
3315 struct apr_hdr hdr;
3316 int rc;
3317
3318 if (!ac || ac->apr == NULL) {
3319 pr_err("%s:APR handle NULL\n", __func__);
3320 return -EINVAL;
3321 }
3322 q6asm_add_hdr_async(ac, &hdr, sizeof(hdr), TRUE);
3323 switch (cmd) {
3324 case CMD_PAUSE:
3325 pr_debug("%s:CMD_PAUSE\n", __func__);
3326 hdr.opcode = ASM_SESSION_CMD_PAUSE;
3327 break;
3328 case CMD_EOS:
3329 pr_debug("%s:CMD_EOS\n", __func__);
3330 hdr.opcode = ASM_DATA_CMD_EOS;
3331 break;
3332 default:
3333 pr_err("%s:Invalid format[%d]\n", __func__, cmd);
3334 goto fail_cmd;
3335 }
3336 pr_debug("%s:session[%d]opcode[0x%x] ", __func__,
3337 ac->session,
3338 hdr.opcode);
3339 rc = apr_send_pkt(ac->apr, (uint32_t *) &hdr);
3340 if (rc < 0) {
3341 pr_err("%s:Commmand 0x%x failed\n", __func__, hdr.opcode);
3342 goto fail_cmd;
3343 }
3344 return 0;
3345fail_cmd:
3346 return -EINVAL;
3347}
3348
3349static void q6asm_reset_buf_state(struct audio_client *ac)
3350{
3351 int cnt = 0;
3352 int loopcnt = 0;
3353 struct audio_port_data *port = NULL;
3354
3355 if (ac->io_mode == SYNC_IO_MODE) {
3356 mutex_lock(&ac->cmd_lock);
3357 for (loopcnt = 0; loopcnt <= OUT; loopcnt++) {
3358 port = &ac->port[loopcnt];
3359 cnt = port->max_buf_cnt - 1;
3360 port->dsp_buf = 0;
3361 port->cpu_buf = 0;
3362 while (cnt >= 0) {
3363 if (!port->buf)
3364 continue;
3365 port->buf[cnt].used = 1;
3366 cnt--;
3367 }
3368 }
3369 mutex_unlock(&ac->cmd_lock);
3370 }
3371}
3372
3373int q6asm_reg_tx_overflow(struct audio_client *ac, uint16_t enable)
3374{
3375 struct asm_session_cmd_regx_overflow tx_overflow;
3376 int rc;
3377
3378 if (!ac || ac->apr == NULL) {
3379 pr_err("APR handle NULL\n");
3380 return -EINVAL;
3381 }
3382 pr_debug("%s:session[%d]enable[%d]\n", __func__,
3383 ac->session, enable);
3384 q6asm_add_hdr(ac, &tx_overflow.hdr, sizeof(tx_overflow), TRUE);
3385
3386 tx_overflow.hdr.opcode = \
3387 ASM_SESSION_CMD_REGISTER_FORX_OVERFLOW_EVENTS;
3388 /* tx overflow event: enable */
3389 tx_overflow.enable_flag = enable;
3390
3391 rc = apr_send_pkt(ac->apr, (uint32_t *) &tx_overflow);
3392 if (rc < 0) {
3393 pr_err("tx overflow op[0x%x]rc[%d]\n", \
3394 tx_overflow.hdr.opcode, rc);
3395 goto fail_cmd;
3396 }
3397 rc = wait_event_timeout(ac->cmd_wait,
3398 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
3399 if (!rc) {
3400 pr_err("timeout. waited for tx overflow\n");
3401 goto fail_cmd;
3402 }
3403 return 0;
3404fail_cmd:
3405 return -EINVAL;
3406}
3407
3408int q6asm_get_apr_service_id(int session_id)
3409{
3410 pr_debug("%s\n", __func__);
3411
3412 if (session_id < 0 || session_id > SESSION_MAX) {
3413 pr_err("%s: invalid session_id = %d\n", __func__, session_id);
3414 return -EINVAL;
3415 }
3416
3417 return ((struct apr_svc *)session[session_id]->apr)->id;
3418}
3419
3420
3421static int __init q6asm_init(void)
3422{
3423 pr_debug("%s\n", __func__);
3424 memset(session, 0, sizeof(session));
3425
3426 config_debug_fs_init();
3427
3428 return 0;
3429}
3430
3431device_initcall(q6asm_init);