blob: 4ec3f9189ffc888e23fa62ad1ed9434e0ed8d725 [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,
Hanumant Singh2ac41c92012-08-29 18:39:44 -0700635 (0x1 << ION_AUDIO_HEAP_ID), 0);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700636 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
Mitchel Humpherys456e2682012-09-12 14:42:50 -0700655 (buf[cnt].client, buf[cnt].handle);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700656 if (IS_ERR_OR_NULL((void *)
657 buf[cnt].data)) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700658 pr_err("%s: ION memory mapping for AUDIO failed\n",
659 __func__);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700660 goto fail;
661 }
662 memset((void *)buf[cnt].data, 0, bufsz);
663 buf[cnt].used = 1;
664 buf[cnt].size = bufsz;
665 buf[cnt].actual_size = bufsz;
666 pr_debug("%s data[%p]phys[%p][%p]\n",
667 __func__,
668 (void *)buf[cnt].data,
669 (void *)buf[cnt].phys,
670 (void *)&buf[cnt].phys);
671 cnt++;
672 }
673 }
674 }
675 ac->port[dir].max_buf_cnt = cnt;
676
677 mutex_unlock(&ac->cmd_lock);
678 rc = q6asm_memory_map_regions(ac, dir, bufsz, cnt);
679 if (rc < 0) {
680 pr_err("%s:CMD Memory_map_regions failed\n", __func__);
681 goto fail;
682 }
683 }
684 return 0;
685fail:
686 q6asm_audio_client_buf_free(dir, ac);
687 return -EINVAL;
688}
689
690int q6asm_audio_client_buf_alloc_contiguous(unsigned int dir,
691 struct audio_client *ac,
692 unsigned int bufsz,
693 unsigned int bufcnt)
694{
695 int cnt = 0;
696 int rc = 0;
697 struct audio_buffer *buf;
698 int len;
699
700 if (!(ac) || ((dir != IN) && (dir != OUT)))
701 return -EINVAL;
702
703 pr_debug("%s: session[%d]bufsz[%d]bufcnt[%d]\n",
704 __func__, ac->session,
705 bufsz, bufcnt);
706
707 if (ac->session <= 0 || ac->session > 8)
708 goto fail;
709
710 if (ac->port[dir].buf) {
711 pr_debug("%s: buffer already allocated\n", __func__);
712 return 0;
713 }
714 mutex_lock(&ac->cmd_lock);
715 buf = kzalloc(((sizeof(struct audio_buffer))*bufcnt),
716 GFP_KERNEL);
717
718 if (!buf) {
719 mutex_unlock(&ac->cmd_lock);
720 goto fail;
721 }
722
723 ac->port[dir].buf = buf;
724
725 buf[0].client = msm_ion_client_create(UINT_MAX, "audio_client");
726 if (IS_ERR_OR_NULL((void *)buf[0].client)) {
727 pr_err("%s: ION create client for AUDIO failed\n", __func__);
728 goto fail;
729 }
730 buf[0].handle = ion_alloc(buf[0].client, bufsz * bufcnt, SZ_4K,
Hanumant Singh2ac41c92012-08-29 18:39:44 -0700731 (0x1 << ION_AUDIO_HEAP_ID), 0);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700732 if (IS_ERR_OR_NULL((void *) buf[0].handle)) {
733 pr_err("%s: ION memory allocation for AUDIO failed\n",
734 __func__);
735 goto fail;
736 }
737
738 rc = ion_phys(buf[0].client, buf[0].handle,
739 (ion_phys_addr_t *)&buf[0].phys, (size_t *)&len);
740 if (rc) {
741 pr_err("%s: ION Get Physical for AUDIO failed, rc = %d\n",
742 __func__, rc);
743 goto fail;
744 }
745
Mitchel Humpherys456e2682012-09-12 14:42:50 -0700746 buf[0].data = ion_map_kernel(buf[0].client, buf[0].handle);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700747 if (IS_ERR_OR_NULL((void *) buf[0].data)) {
748 pr_err("%s: ION memory mapping for AUDIO failed\n", __func__);
749 goto fail;
750 }
751 memset((void *)buf[0].data, 0, (bufsz * bufcnt));
752 if (!buf[0].data) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700753 pr_err("%s:invalid vaddr, iomap failed\n", __func__);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700754 mutex_unlock(&ac->cmd_lock);
755 goto fail;
756 }
757
758 buf[0].used = dir ^ 1;
759 buf[0].size = bufsz;
760 buf[0].actual_size = bufsz;
761 cnt = 1;
762 while (cnt < bufcnt) {
763 if (bufsz > 0) {
764 buf[cnt].data = buf[0].data + (cnt * bufsz);
765 buf[cnt].phys = buf[0].phys + (cnt * bufsz);
766 if (!buf[cnt].data) {
767 pr_err("%s Buf alloc failed\n",
768 __func__);
769 mutex_unlock(&ac->cmd_lock);
770 goto fail;
771 }
772 buf[cnt].used = dir ^ 1;
773 buf[cnt].size = bufsz;
774 buf[cnt].actual_size = bufsz;
775 pr_debug("%s data[%p]phys[%p][%p]\n", __func__,
776 (void *)buf[cnt].data,
777 (void *)buf[cnt].phys,
778 (void *)&buf[cnt].phys);
779 }
780 cnt++;
781 }
782 ac->port[dir].max_buf_cnt = cnt;
783 mutex_unlock(&ac->cmd_lock);
784 rc = q6asm_memory_map_regions(ac, dir, bufsz, cnt);
785 if (rc < 0) {
786 pr_err("%s:CMD Memory_map_regions failed\n", __func__);
787 goto fail;
788 }
789 return 0;
790fail:
791 q6asm_audio_client_buf_free_contiguous(dir, ac);
792 return -EINVAL;
793}
794
795static int32_t q6asm_mmapcallback(struct apr_client_data *data, void *priv)
796{
797 uint32_t sid = 0;
798 uint32_t dir = 0;
799 uint32_t *payload = data->payload;
800 unsigned long dsp_flags;
801
802 struct audio_client *ac = NULL;
803 struct audio_port_data *port;
804
805 if (!data) {
806 pr_err("%s: Invalid CB\n", __func__);
807 return 0;
808 }
809 if (data->opcode == RESET_EVENTS) {
810 pr_debug("%s: Reset event is received: %d %d apr[%p]\n",
811 __func__,
812 data->reset_event,
813 data->reset_proc,
814 this_mmap.apr);
815 apr_reset(this_mmap.apr);
816 atomic_set(&this_mmap.ref_cnt, 0);
817 this_mmap.apr = NULL;
818 return 0;
819 }
820 sid = (data->token >> 8) & 0x0F;
821 ac = q6asm_get_audio_client(sid);
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700822 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 -0700823 __func__, payload[0], payload[1], data->opcode, data->token,
824 data->payload_size, data->src_port, data->dest_port, sid, dir);
825 pr_debug("%s:Payload = [0x%x] status[0x%x]\n",
826 __func__, payload[0], payload[1]);
827
828 if (data->opcode == APR_BASIC_RSP_RESULT) {
829 switch (payload[0]) {
830 case ASM_CMD_SHARED_MEM_MAP_REGIONS:
831 case ASM_CMD_SHARED_MEM_UNMAP_REGIONS:
832 if (atomic_read(&ac->cmd_state)) {
833 atomic_set(&ac->cmd_state, 0);
834 wake_up(&ac->cmd_wait);
835 }
836 pr_debug("%s:Payload = [0x%x] status[0x%x]\n",
837 __func__, payload[0], payload[1]);
838 break;
839 default:
840 pr_debug("%s:command[0x%x] not expecting rsp\n",
841 __func__, payload[0]);
842 break;
843 }
844 return 0;
845 }
846
847 dir = (data->token & 0x0F);
848 port = &ac->port[dir];
849
850 switch (data->opcode) {
851 case ASM_CMDRSP_SHARED_MEM_MAP_REGIONS:{
852 pr_debug("%s:PL#0[0x%x]PL#1 [0x%x] dir=%x s_id=%x\n",
853 __func__, payload[0], payload[1], dir, sid);
854 spin_lock_irqsave(&port->dsp_lock, dsp_flags);
855 if (atomic_read(&ac->cmd_state)) {
856 ac->port[dir].tmp_hdl = payload[0];
857 atomic_set(&ac->cmd_state, 0);
858 wake_up(&ac->cmd_wait);
859 }
860 spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
861 break;
862 }
863 case ASM_CMD_SHARED_MEM_UNMAP_REGIONS:{
864 pr_debug("%s:PL#0[0x%x]PL#1 [0x%x]\n",
865 __func__, payload[0], payload[1]);
866 spin_lock_irqsave(&port->dsp_lock, dsp_flags);
867 if (atomic_read(&ac->cmd_state)) {
868 atomic_set(&ac->cmd_state, 0);
869 wake_up(&ac->cmd_wait);
870 }
871 spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
872
873 break;
874 }
875 default:
876 pr_debug("%s:command[0x%x]success [0x%x]\n",
877 __func__, payload[0], payload[1]);
878 }
879 if (ac->cb)
880 ac->cb(data->opcode, data->token,
881 data->payload, ac->priv);
882 return 0;
883}
884
885
886static int32_t q6asm_callback(struct apr_client_data *data, void *priv)
887{
888 int i = 0;
889 struct audio_client *ac = (struct audio_client *)priv;
890 uint32_t token;
891 unsigned long dsp_flags;
892 uint32_t *payload;
893
894
895 if ((ac == NULL) || (data == NULL)) {
896 pr_err("ac or priv NULL\n");
897 return -EINVAL;
898 }
899 if (ac->session <= 0 || ac->session > 8) {
900 pr_err("%s:Session ID is invalid, session = %d\n", __func__,
901 ac->session);
902 return -EINVAL;
903 }
904
905 payload = data->payload;
906
907 if (data->opcode == RESET_EVENTS) {
908 pr_debug("q6asm_callback: Reset event is received: %d %d apr[%p]\n",
909 data->reset_event, data->reset_proc, ac->apr);
910 if (ac->cb)
911 ac->cb(data->opcode, data->token,
912 (uint32_t *)data->payload, ac->priv);
913 apr_reset(ac->apr);
914 return 0;
915 }
916
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700917 pr_debug("%s: session[%d]opcode[0x%x] token[0x%x]payload_s[%d] src[%d] dest[%d]\n",
918 __func__,
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -0700919 ac->session, data->opcode,
920 data->token, data->payload_size, data->src_port,
921 data->dest_port);
922 if ((data->opcode != ASM_DATA_EVENT_RENDERED_EOS) &&
923 (data->opcode != ASM_DATA_EVENT_EOS))
924 pr_debug("%s:Payload = [0x%x] status[0x%x]\n",
925 __func__, payload[0], payload[1]);
926 if (data->opcode == APR_BASIC_RSP_RESULT) {
927 token = data->token;
928 switch (payload[0]) {
929 case ASM_STREAM_CMD_SET_PP_PARAMS_V2:
930 if (rtac_make_asm_callback(ac->session, payload,
931 data->payload_size))
932 break;
933 case ASM_SESSION_CMD_PAUSE:
934 case ASM_DATA_CMD_EOS:
935 case ASM_STREAM_CMD_CLOSE:
936 case ASM_STREAM_CMD_FLUSH:
937 case ASM_SESSION_CMD_RUN_V2:
938 case ASM_SESSION_CMD_REGISTER_FORX_OVERFLOW_EVENTS:
939 case ASM_STREAM_CMD_FLUSH_READBUFS:
940 pr_debug("%s:Payload = [0x%x]\n", __func__, payload[0]);
941 if (token != ac->session) {
942 pr_err("%s:Invalid session[%d] rxed expected[%d]",
943 __func__, token, ac->session);
944 return -EINVAL;
945 }
946 case ASM_STREAM_CMD_OPEN_READ_V2:
947 case ASM_STREAM_CMD_OPEN_WRITE_V2:
948 case ASM_STREAM_CMD_OPEN_READWRITE_V2:
949 case ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2:
950 case ASM_STREAM_CMD_SET_ENCDEC_PARAM:
951 pr_debug("%s:Payload = [0x%x]stat[0x%x]\n",
952 __func__, payload[0], payload[1]);
953 if (atomic_read(&ac->cmd_state)) {
954 atomic_set(&ac->cmd_state, 0);
955 wake_up(&ac->cmd_wait);
956 }
957 if (ac->cb)
958 ac->cb(data->opcode, data->token,
959 (uint32_t *)data->payload, ac->priv);
960 break;
961 default:
962 pr_debug("%s:command[0x%x] not expecting rsp\n",
963 __func__, payload[0]);
964 break;
965 }
966 return 0;
967 }
968
969 switch (data->opcode) {
970 case ASM_DATA_EVENT_WRITE_DONE_V2:{
971 struct audio_port_data *port = &ac->port[IN];
972 pr_debug("%s: Rxed opcode[0x%x] status[0x%x] token[%d]",
973 __func__, payload[0], payload[1],
974 data->token);
975 if (ac->io_mode == SYNC_IO_MODE) {
976 if (port->buf == NULL) {
977 pr_err("%s: Unexpected Write Done\n",
978 __func__);
979 return -EINVAL;
980 }
981 spin_lock_irqsave(&port->dsp_lock, dsp_flags);
982 if (port->buf[data->token].phys !=
983 payload[0]) {
984 pr_err("Buf expected[%p]rxed[%p]\n",\
985 (void *)port->buf[data->token].phys,\
986 (void *)payload[0]);
987 spin_unlock_irqrestore(&port->dsp_lock,
988 dsp_flags);
989 return -EINVAL;
990 }
991 token = data->token;
992 port->buf[token].used = 1;
993 spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
994
995 config_debug_fs_write_cb();
996
997 for (i = 0; i < port->max_buf_cnt; i++)
998 pr_debug("%d ", port->buf[i].used);
999
1000 }
1001 break;
1002 }
1003 case ASM_STREAM_CMDRSP_GET_PP_PARAMS_V2:
1004 rtac_make_asm_callback(ac->session, payload,
1005 data->payload_size);
1006 break;
1007 case ASM_DATA_EVENT_READ_DONE_V2:{
1008
1009 struct audio_port_data *port = &ac->port[OUT];
1010
1011 config_debug_fs_read_cb();
1012
1013 pr_debug("%s:R-D: status=%d buff_add=%x act_size=%d offset=%d\n",
1014 __func__, payload[READDONE_IDX_STATUS],
1015 payload[READDONE_IDX_BUFADD_LSW],
1016 payload[READDONE_IDX_SIZE],
1017 payload[READDONE_IDX_OFFSET]);
1018
1019 pr_debug("%s:R-D:msw_ts=%d lsw_ts=%d memmap_hdl=%x flags=%d id=%d num=%d\n",
1020 __func__, payload[READDONE_IDX_MSW_TS],
1021 payload[READDONE_IDX_LSW_TS],
1022 payload[READDONE_IDX_MEMMAP_HDL],
1023 payload[READDONE_IDX_FLAGS],
1024 payload[READDONE_IDX_SEQ_ID],
1025 payload[READDONE_IDX_NUMFRAMES]);
1026
1027 if (ac->io_mode == SYNC_IO_MODE) {
1028 if (port->buf == NULL) {
1029 pr_err("%s: Unexpected Write Done\n", __func__);
1030 return -EINVAL;
1031 }
1032 spin_lock_irqsave(&port->dsp_lock, dsp_flags);
1033 token = data->token;
1034 port->buf[token].used = 0;
1035 if (port->buf[token].phys !=
1036 payload[READDONE_IDX_BUFADD_LSW]) {
1037 pr_err("Buf expected[%p]rxed[%p]\n",\
1038 (void *)port->buf[token].phys,\
1039 (void *)payload[READDONE_IDX_BUFADD_LSW]);
1040 spin_unlock_irqrestore(&port->dsp_lock,
1041 dsp_flags);
1042 break;
1043 }
1044 port->buf[token].actual_size =
1045 payload[READDONE_IDX_SIZE];
1046 spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
1047 }
1048 break;
1049 }
1050 case ASM_DATA_EVENT_EOS:
1051 case ASM_DATA_EVENT_RENDERED_EOS:
1052 pr_debug("%s:EOS ACK received: rxed opcode[0x%x]\n",
1053 __func__, data->opcode);
1054 break;
1055 case ASM_SESSION_EVENTX_OVERFLOW:
1056 pr_err("ASM_SESSION_EVENTX_OVERFLOW\n");
1057 break;
1058 case ASM_SESSION_CMDRSP_GET_SESSIONTIME_V3:
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001059 pr_debug("%s: ASM_SESSION_CMDRSP_GET_SESSIONTIME_V3, payload[0] = %d, payload[1] = %d, payload[2] = %d\n",
1060 __func__,
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001061 payload[0], payload[1], payload[2]);
Harmandeep Singh1b712ec2012-08-31 16:28:38 -07001062 ac->time_stamp = (uint64_t)(((uint64_t)payload[2] << 32) |
1063 payload[1]);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001064 if (atomic_read(&ac->cmd_state)) {
1065 atomic_set(&ac->cmd_state, 0);
1066 wake_up(&ac->cmd_wait);
1067 }
1068 break;
1069 case ASM_DATA_EVENT_SR_CM_CHANGE_NOTIFY:
1070 case ASM_DATA_EVENT_ENC_SR_CM_CHANGE_NOTIFY:
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001071 pr_debug("%s: ASM_DATA_EVENT_SR_CM_CHANGE_NOTIFY, payload[0] = %d, payload[1] = %d, payload[2] = %d, payload[3] = %d\n",
1072 __func__,
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001073 payload[0], payload[1], payload[2],
1074 payload[3]);
1075 break;
1076 }
1077 if (ac->cb)
1078 ac->cb(data->opcode, data->token,
1079 data->payload, ac->priv);
1080
1081 return 0;
1082}
1083
1084void *q6asm_is_cpu_buf_avail(int dir, struct audio_client *ac, uint32_t *size,
1085 uint32_t *index)
1086{
1087 void *data;
1088 unsigned char idx;
1089 struct audio_port_data *port;
1090
1091 if (!ac || ((dir != IN) && (dir != OUT)))
1092 return NULL;
1093
1094 if (ac->io_mode == SYNC_IO_MODE) {
1095 port = &ac->port[dir];
1096
1097 mutex_lock(&port->lock);
1098 idx = port->cpu_buf;
1099 if (port->buf == NULL) {
1100 pr_debug("%s:Buffer pointer null\n", __func__);
1101 mutex_unlock(&port->lock);
1102 return NULL;
1103 }
1104 /* dir 0: used = 0 means buf in use
1105 dir 1: used = 1 means buf in use */
1106 if (port->buf[idx].used == dir) {
1107 /* To make it more robust, we could loop and get the
1108 next avail buf, its risky though */
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001109 pr_debug("%s:Next buf idx[0x%x] not available, dir[%d]\n",
1110 __func__, idx, dir);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001111 mutex_unlock(&port->lock);
1112 return NULL;
1113 }
1114 *size = port->buf[idx].actual_size;
1115 *index = port->cpu_buf;
1116 data = port->buf[idx].data;
1117 pr_debug("%s:session[%d]index[%d] data[%p]size[%d]\n",
1118 __func__,
1119 ac->session,
1120 port->cpu_buf,
1121 data, *size);
1122 /* By default increase the cpu_buf cnt
1123 user accesses this function,increase cpu
1124 buf(to avoid another api)*/
1125 port->buf[idx].used = dir;
1126 port->cpu_buf = ((port->cpu_buf + 1) & (port->max_buf_cnt - 1));
1127 mutex_unlock(&port->lock);
1128 return data;
1129 }
1130 return NULL;
1131}
1132
1133void *q6asm_is_cpu_buf_avail_nolock(int dir, struct audio_client *ac,
1134 uint32_t *size, uint32_t *index)
1135{
1136 void *data;
1137 unsigned char idx;
1138 struct audio_port_data *port;
1139
1140 if (!ac || ((dir != IN) && (dir != OUT)))
1141 return NULL;
1142
1143 port = &ac->port[dir];
1144
1145 idx = port->cpu_buf;
1146 if (port->buf == NULL) {
1147 pr_debug("%s:Buffer pointer null\n", __func__);
1148 return NULL;
1149 }
1150 /*
1151 * dir 0: used = 0 means buf in use
1152 * dir 1: used = 1 means buf in use
1153 */
1154 if (port->buf[idx].used == dir) {
1155 /*
1156 * To make it more robust, we could loop and get the
1157 * next avail buf, its risky though
1158 */
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001159 pr_debug("%s:Next buf idx[0x%x] not available, dir[%d]\n",
1160 __func__, idx, dir);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001161 return NULL;
1162 }
1163 *size = port->buf[idx].actual_size;
1164 *index = port->cpu_buf;
1165 data = port->buf[idx].data;
1166 pr_debug("%s:session[%d]index[%d] data[%p]size[%d]\n",
1167 __func__, ac->session, port->cpu_buf,
1168 data, *size);
1169 /*
1170 * By default increase the cpu_buf cnt
1171 * user accesses this function,increase cpu
1172 * buf(to avoid another api)
1173 */
1174 port->buf[idx].used = dir;
1175 port->cpu_buf = ((port->cpu_buf + 1) & (port->max_buf_cnt - 1));
1176 return data;
1177}
1178
1179int q6asm_is_dsp_buf_avail(int dir, struct audio_client *ac)
1180{
1181 int ret = -1;
1182 struct audio_port_data *port;
1183 uint32_t idx;
1184
1185 if (!ac || (dir != OUT))
1186 return ret;
1187
1188 if (ac->io_mode == SYNC_IO_MODE) {
1189 port = &ac->port[dir];
1190
1191 mutex_lock(&port->lock);
1192 idx = port->dsp_buf;
1193
1194 if (port->buf[idx].used == (dir ^ 1)) {
1195 /* To make it more robust, we could loop and get the
1196 next avail buf, its risky though */
1197 pr_err("Next buf idx[0x%x] not available, dir[%d]\n",
1198 idx, dir);
1199 mutex_unlock(&port->lock);
1200 return ret;
1201 }
1202 pr_debug("%s: session[%d]dsp_buf=%d cpu_buf=%d\n", __func__,
1203 ac->session, port->dsp_buf, port->cpu_buf);
1204 ret = ((port->dsp_buf != port->cpu_buf) ? 0 : -1);
1205 mutex_unlock(&port->lock);
1206 }
1207 return ret;
1208}
1209
1210static void q6asm_add_hdr(struct audio_client *ac, struct apr_hdr *hdr,
1211 uint32_t pkt_size, uint32_t cmd_flg)
1212{
1213 pr_debug("%s:pkt_size=%d cmd_flg=%d session=%d\n", __func__, pkt_size,
1214 cmd_flg, ac->session);
1215 mutex_lock(&ac->cmd_lock);
1216 hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, \
1217 APR_HDR_LEN(sizeof(struct apr_hdr)),\
1218 APR_PKT_VER);
1219 hdr->src_svc = ((struct apr_svc *)ac->apr)->id;
1220 hdr->src_domain = APR_DOMAIN_APPS;
1221 hdr->dest_svc = APR_SVC_ASM;
1222 hdr->dest_domain = APR_DOMAIN_ADSP;
1223 hdr->src_port = ((ac->session << 8) & 0xFF00) | 0x01;
1224 hdr->dest_port = ((ac->session << 8) & 0xFF00) | 0x01;
1225 if (cmd_flg) {
1226 hdr->token = ac->session;
1227 atomic_set(&ac->cmd_state, 1);
1228 }
1229 hdr->pkt_size = pkt_size;
1230 mutex_unlock(&ac->cmd_lock);
1231 return;
1232}
1233
1234static void q6asm_add_hdr_async(struct audio_client *ac, struct apr_hdr *hdr,
1235 uint32_t pkt_size, uint32_t cmd_flg)
1236{
1237 pr_debug("pkt_size = %d, cmd_flg = %d, session = %d\n",
1238 pkt_size, cmd_flg, ac->session);
1239 hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, \
1240 APR_HDR_LEN(sizeof(struct apr_hdr)),\
1241 APR_PKT_VER);
1242 hdr->src_svc = ((struct apr_svc *)ac->apr)->id;
1243 hdr->src_domain = APR_DOMAIN_APPS;
1244 hdr->dest_svc = APR_SVC_ASM;
1245 hdr->dest_domain = APR_DOMAIN_ADSP;
1246 hdr->src_port = ((ac->session << 8) & 0xFF00) | 0x01;
1247 hdr->dest_port = ((ac->session << 8) & 0xFF00) | 0x01;
1248 if (cmd_flg) {
1249 hdr->token = ac->session;
1250 atomic_set(&ac->cmd_state, 1);
1251 }
1252 hdr->pkt_size = pkt_size;
1253 return;
1254}
1255
1256static void q6asm_add_mmaphdr(struct audio_client *ac, struct apr_hdr *hdr,
1257 u32 pkt_size, u32 cmd_flg, u32 token)
1258{
1259 pr_debug("%s:pkt size=%d cmd_flg=%d\n", __func__, pkt_size, cmd_flg);
1260 hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, \
1261 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
1262 hdr->src_port = 0;
1263 hdr->dest_port = 0;
1264 if (cmd_flg) {
1265 hdr->token = token;
1266 atomic_set(&ac->cmd_state, 1);
1267 }
1268 hdr->pkt_size = pkt_size;
1269 return;
1270}
1271int q6asm_open_read(struct audio_client *ac,
1272 uint32_t format)
1273{
1274 int rc = 0x00;
1275 struct asm_stream_cmd_open_read_v2 open;
1276
1277 uint16_t bits_per_sample = 16;
1278
1279
1280 config_debug_fs_reset_index();
1281
1282 if ((ac == NULL) || (ac->apr == NULL)) {
1283 pr_err("%s: APR handle NULL\n", __func__);
1284 return -EINVAL;
1285 }
1286 pr_debug("%s:session[%d]", __func__, ac->session);
1287
1288 q6asm_add_hdr(ac, &open.hdr, sizeof(open), TRUE);
1289 open.hdr.opcode = ASM_STREAM_CMD_OPEN_READ_V2;
1290 /* Stream prio : High, provide meta info with encoded frames */
1291 open.src_endpointype = ASM_END_POINT_DEVICE_MATRIX;
1292
1293 open.preprocopo_id = get_asm_topology();
1294 if (open.preprocopo_id == 0)
1295 open.preprocopo_id = ASM_STREAM_POSTPROC_TOPO_ID_DEFAULT;
1296 open.bits_per_sample = bits_per_sample;
1297
1298 switch (format) {
1299 case FORMAT_LINEAR_PCM:
1300 open.mode_flags = 0x00;
1301 open.enc_cfg_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V2;
1302 break;
1303 case FORMAT_MPEG4_AAC:
1304 open.mode_flags = BUFFER_META_ENABLE;
1305 open.enc_cfg_id = ASM_MEDIA_FMT_AAC_V2;
1306 break;
1307 case FORMAT_V13K:
1308 open.mode_flags = BUFFER_META_ENABLE;
1309 open.enc_cfg_id = ASM_MEDIA_FMT_V13K_FS;
1310 break;
1311 case FORMAT_EVRC:
1312 open.mode_flags = BUFFER_META_ENABLE;
1313 open.enc_cfg_id = ASM_MEDIA_FMT_EVRC_FS;
1314 break;
1315 case FORMAT_AMRNB:
1316 open.mode_flags = BUFFER_META_ENABLE ;
1317 open.enc_cfg_id = ASM_MEDIA_FMT_AMRNB_FS;
1318 break;
1319 case FORMAT_AMRWB:
1320 open.mode_flags = BUFFER_META_ENABLE ;
1321 open.enc_cfg_id = ASM_MEDIA_FMT_AMRWB_FS;
1322 break;
1323 default:
1324 pr_err("Invalid format[%d]\n", format);
1325 goto fail_cmd;
1326 }
1327 rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
1328 if (rc < 0) {
1329 pr_err("open failed op[0x%x]rc[%d]\n", \
1330 open.hdr.opcode, rc);
1331 goto fail_cmd;
1332 }
1333 rc = wait_event_timeout(ac->cmd_wait,
1334 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1335 if (!rc) {
1336 pr_err("%s: timeout. waited for open read rc[%d]\n", __func__,
1337 rc);
1338 goto fail_cmd;
1339 }
1340 return 0;
1341fail_cmd:
1342 return -EINVAL;
1343}
1344int q6asm_open_write(struct audio_client *ac, uint32_t format)
1345{
1346 int rc = 0x00;
1347 struct asm_stream_cmd_open_write_v2 open;
1348
1349 if ((ac == NULL) || (ac->apr == NULL)) {
1350 pr_err("%s: APR handle NULL\n", __func__);
1351 return -EINVAL;
1352 }
1353 pr_debug("%s: session[%d] wr_format[0x%x]", __func__, ac->session,
1354 format);
1355
1356 q6asm_add_hdr(ac, &open.hdr, sizeof(open), TRUE);
1357
1358 open.hdr.opcode = ASM_STREAM_CMD_OPEN_WRITE_V2;
1359 open.mode_flags = 0x00;
1360 /* source endpoint : matrix */
1361 open.sink_endpointype = ASM_END_POINT_DEVICE_MATRIX;
1362 open.bits_per_sample = 16;
1363
1364 open.postprocopo_id = get_asm_topology();
1365 if (open.postprocopo_id == 0)
1366 open.postprocopo_id = ASM_STREAM_POSTPROC_TOPO_ID_DEFAULT;
1367
1368 switch (format) {
1369 case FORMAT_LINEAR_PCM:
1370 open.dec_fmt_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V2;
1371 break;
1372 case FORMAT_MPEG4_AAC:
1373 open.dec_fmt_id = ASM_MEDIA_FMT_AAC_V2;
1374 break;
1375 case FORMAT_MPEG4_MULTI_AAC:
1376 open.dec_fmt_id = ASM_MEDIA_FMT_DOLBY_AAC;
1377 break;
1378 case FORMAT_WMA_V9:
1379 open.dec_fmt_id = ASM_MEDIA_FMT_WMA_V9_V2;
1380 break;
1381 case FORMAT_WMA_V10PRO:
1382 open.dec_fmt_id = ASM_MEDIA_FMT_WMA_V10PRO_V2;
1383 break;
1384 case FORMAT_MP3:
1385 open.dec_fmt_id = ASM_MEDIA_FMT_MP3;
1386 break;
1387 default:
1388 pr_err("%s: Invalid format[%d]\n", __func__, format);
1389 goto fail_cmd;
1390 }
1391 rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
1392 if (rc < 0) {
1393 pr_err("%s: open failed op[0x%x]rc[%d]\n", \
1394 __func__, open.hdr.opcode, rc);
1395 goto fail_cmd;
1396 }
1397 rc = wait_event_timeout(ac->cmd_wait,
1398 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1399 if (!rc) {
1400 pr_err("%s: timeout. waited for open write rc[%d]\n", __func__,
1401 rc);
1402 goto fail_cmd;
1403 }
1404 return 0;
1405fail_cmd:
1406 return -EINVAL;
1407}
1408
1409int q6asm_open_read_write(struct audio_client *ac,
1410 uint32_t rd_format,
1411 uint32_t wr_format)
1412{
1413 int rc = 0x00;
1414 struct asm_stream_cmd_open_readwrite_v2 open;
1415
1416 if ((ac == NULL) || (ac->apr == NULL)) {
1417 pr_err("APR handle NULL\n");
1418 return -EINVAL;
1419 }
1420 pr_debug("%s: session[%d]", __func__, ac->session);
1421 pr_debug("wr_format[0x%x]rd_format[0x%x]",
1422 wr_format, rd_format);
1423
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001424 ac->io_mode |= NT_MODE;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001425 q6asm_add_hdr(ac, &open.hdr, sizeof(open), TRUE);
1426 open.hdr.opcode = ASM_STREAM_CMD_OPEN_READWRITE_V2;
1427
1428 open.mode_flags = BUFFER_META_ENABLE;
1429 open.bits_per_sample = 16;
1430 /* source endpoint : matrix */
1431 open.postprocopo_id = get_asm_topology();
1432 if (open.postprocopo_id == 0)
1433 open.postprocopo_id = ASM_STREAM_POSTPROC_TOPO_ID_DEFAULT;
1434
1435 switch (wr_format) {
1436 case FORMAT_LINEAR_PCM:
1437 open.dec_fmt_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V2;
1438 break;
1439 case FORMAT_MPEG4_AAC:
1440 open.dec_fmt_id = ASM_MEDIA_FMT_AAC_V2;
1441 break;
1442 case FORMAT_MPEG4_MULTI_AAC:
1443 open.dec_fmt_id = ASM_MEDIA_FMT_DOLBY_AAC;
1444 break;
1445 case FORMAT_WMA_V9:
1446 open.dec_fmt_id = ASM_MEDIA_FMT_WMA_V9_V2;
1447 break;
1448 case FORMAT_WMA_V10PRO:
1449 open.dec_fmt_id = ASM_MEDIA_FMT_WMA_V10PRO_V2;
1450 break;
1451 case FORMAT_AMRNB:
1452 open.dec_fmt_id = ASM_MEDIA_FMT_AMRNB_FS;
1453 break;
1454 case FORMAT_AMRWB:
1455 open.dec_fmt_id = ASM_MEDIA_FMT_AMRWB_FS;
1456 break;
1457 case FORMAT_V13K:
1458 open.dec_fmt_id = ASM_MEDIA_FMT_V13K_FS;
1459 break;
1460 case FORMAT_EVRC:
1461 open.dec_fmt_id = ASM_MEDIA_FMT_EVRC_FS;
1462 break;
1463 case FORMAT_EVRCB:
1464 open.dec_fmt_id = ASM_MEDIA_FMT_EVRCB_FS;
1465 break;
1466 case FORMAT_EVRCWB:
1467 open.dec_fmt_id = ASM_MEDIA_FMT_EVRCWB_FS;
1468 break;
1469 case FORMAT_MP3:
1470 open.dec_fmt_id = ASM_MEDIA_FMT_MP3;
1471 break;
1472 default:
1473 pr_err("Invalid format[%d]\n", wr_format);
1474 goto fail_cmd;
1475 }
1476
1477 switch (rd_format) {
1478 case FORMAT_LINEAR_PCM:
1479 open.enc_cfg_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V2;
1480 break;
1481 case FORMAT_MPEG4_AAC:
1482 open.enc_cfg_id = ASM_MEDIA_FMT_AAC_V2;
1483 break;
1484 case FORMAT_V13K:
1485 open.enc_cfg_id = ASM_MEDIA_FMT_V13K_FS;
1486 break;
1487 case FORMAT_EVRC:
1488 open.enc_cfg_id = ASM_MEDIA_FMT_EVRC_FS;
1489 break;
1490 case FORMAT_AMRNB:
1491 open.enc_cfg_id = ASM_MEDIA_FMT_AMRNB_FS;
1492 break;
1493 case FORMAT_AMRWB:
1494 open.enc_cfg_id = ASM_MEDIA_FMT_AMRWB_FS;
1495 break;
1496 default:
1497 pr_err("Invalid format[%d]\n", rd_format);
1498 goto fail_cmd;
1499 }
1500 pr_debug("%s:rdformat[0x%x]wrformat[0x%x]\n", __func__,
1501 open.enc_cfg_id, open.dec_fmt_id);
1502
1503 rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
1504 if (rc < 0) {
1505 pr_err("open failed op[0x%x]rc[%d]\n", \
1506 open.hdr.opcode, rc);
1507 goto fail_cmd;
1508 }
1509 rc = wait_event_timeout(ac->cmd_wait,
1510 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1511 if (!rc) {
1512 pr_err("timeout. waited for open read-write rc[%d]\n", rc);
1513 goto fail_cmd;
1514 }
1515 return 0;
1516fail_cmd:
1517 return -EINVAL;
1518}
1519
1520int q6asm_run(struct audio_client *ac, uint32_t flags,
1521 uint32_t msw_ts, uint32_t lsw_ts)
1522{
1523 struct asm_session_cmd_run_v2 run;
1524 int rc;
1525 if (!ac || ac->apr == NULL) {
1526 pr_err("APR handle NULL\n");
1527 return -EINVAL;
1528 }
1529 pr_debug("%s session[%d]", __func__, ac->session);
1530 q6asm_add_hdr(ac, &run.hdr, sizeof(run), TRUE);
1531
1532 run.hdr.opcode = ASM_SESSION_CMD_RUN_V2;
1533 run.flags = flags;
1534 run.time_lsw = lsw_ts;
1535 run.time_msw = msw_ts;
1536
1537 config_debug_fs_run();
1538
1539 rc = apr_send_pkt(ac->apr, (uint32_t *) &run);
1540 if (rc < 0) {
1541 pr_err("Commmand run failed[%d]", rc);
1542 goto fail_cmd;
1543 }
1544
1545 rc = wait_event_timeout(ac->cmd_wait,
1546 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1547 if (!rc) {
1548 pr_err("timeout. waited for run success rc[%d]", rc);
1549 goto fail_cmd;
1550 }
1551
1552 return 0;
1553fail_cmd:
1554 return -EINVAL;
1555}
1556
1557int q6asm_run_nowait(struct audio_client *ac, uint32_t flags,
1558 uint32_t msw_ts, uint32_t lsw_ts)
1559{
1560 struct asm_session_cmd_run_v2 run;
1561 int rc;
1562 if (!ac || ac->apr == NULL) {
1563 pr_err("%s:APR handle NULL\n", __func__);
1564 return -EINVAL;
1565 }
1566 pr_debug("session[%d]", ac->session);
1567 q6asm_add_hdr_async(ac, &run.hdr, sizeof(run), TRUE);
1568
1569 run.hdr.opcode = ASM_SESSION_CMD_RUN_V2;
1570 run.flags = flags;
1571 run.time_lsw = lsw_ts;
1572 run.time_msw = msw_ts;
1573
1574 rc = apr_send_pkt(ac->apr, (uint32_t *) &run);
1575 if (rc < 0) {
1576 pr_err("%s:Commmand run failed[%d]", __func__, rc);
1577 return -EINVAL;
1578 }
1579 return 0;
1580}
1581
1582
1583int q6asm_enc_cfg_blk_aac(struct audio_client *ac,
1584 uint32_t frames_per_buf,
1585 uint32_t sample_rate, uint32_t channels,
1586 uint32_t bit_rate, uint32_t mode, uint32_t format)
1587{
1588 struct asm_aac_enc_cfg_v2 enc_cfg;
1589 int rc = 0;
1590
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001591 pr_debug("%s:session[%d]frames[%d]SR[%d]ch[%d]bitrate[%d]mode[%d] format[%d]",
1592 __func__, ac->session, frames_per_buf,
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001593 sample_rate, channels, bit_rate, mode, format);
1594
1595 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
1596
1597 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1598 enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
1599 enc_cfg.encdec.param_size = sizeof(struct asm_aac_enc_cfg_v2) -
1600 sizeof(struct asm_stream_cmd_set_encdec_param);
1601 enc_cfg.encblk.frames_per_buf = frames_per_buf;
1602 enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
1603 sizeof(struct asm_enc_cfg_blk_param_v2);
1604 enc_cfg.bit_rate = bit_rate;
1605 enc_cfg.enc_mode = mode;
1606 enc_cfg.aac_fmt_flag = format;
1607 enc_cfg.channel_cfg = channels;
1608 enc_cfg.sample_rate = sample_rate;
1609
1610 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
1611 if (rc < 0) {
1612 pr_err("Comamnd %d failed\n", ASM_STREAM_CMD_SET_ENCDEC_PARAM);
1613 rc = -EINVAL;
1614 goto fail_cmd;
1615 }
1616 rc = wait_event_timeout(ac->cmd_wait,
1617 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1618 if (!rc) {
1619 pr_err("timeout. waited for FORMAT_UPDATE\n");
1620 goto fail_cmd;
1621 }
1622 return 0;
1623fail_cmd:
1624 return -EINVAL;
1625}
1626
1627int q6asm_set_encdec_chan_map(struct audio_client *ac,
1628 uint32_t num_channels)
1629{
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001630 struct asm_dec_out_chan_map_param chan_map;
1631 u8 *channel_mapping;
1632 int rc = 0;
1633 pr_debug("%s: Session %d, num_channels = %d\n",
1634 __func__, ac->session, num_channels);
1635 q6asm_add_hdr(ac, &chan_map.hdr, sizeof(chan_map), TRUE);
1636 chan_map.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1637 chan_map.encdec.param_id = ASM_PARAM_ID_DEC_OUTPUT_CHAN_MAP;
1638 chan_map.encdec.param_size = sizeof(struct asm_dec_out_chan_map_param) -
1639 (sizeof(struct apr_hdr) +
1640 sizeof(struct asm_stream_cmd_set_encdec_param));
1641 chan_map.num_channels = num_channels;
1642 channel_mapping = chan_map.channel_mapping;
1643 memset(channel_mapping, PCM_CHANNEL_NULL, MAX_CHAN_MAP_CHANNELS);
1644 if (q6asm_map_channels(channel_mapping, num_channels))
1645 return -EINVAL;
1646
1647 rc = apr_send_pkt(ac->apr, (uint32_t *) &chan_map);
1648 if (rc < 0) {
1649 pr_err("%s:Command opcode[0x%x]paramid[0x%x] failed\n",
1650 __func__, ASM_STREAM_CMD_SET_ENCDEC_PARAM,
1651 ASM_PARAM_ID_DEC_OUTPUT_CHAN_MAP);
1652 goto fail_cmd;
1653 }
1654 rc = wait_event_timeout(ac->cmd_wait,
1655 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1656 if (!rc) {
1657 pr_err("%s:timeout opcode[0x%x]\n", __func__,
1658 chan_map.hdr.opcode);
1659 rc = -ETIMEDOUT;
1660 goto fail_cmd;
1661 }
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001662 return 0;
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001663fail_cmd:
1664 return rc;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001665}
1666
1667int q6asm_enc_cfg_blk_pcm(struct audio_client *ac,
1668 uint32_t rate, uint32_t channels)
1669{
1670 struct asm_multi_channel_pcm_enc_cfg_v2 enc_cfg;
1671 u8 *channel_mapping;
1672 u32 frames_per_buf = 0;
1673
1674 int rc = 0;
1675
1676 pr_debug("%s: Session %d, rate = %d, channels = %d\n", __func__,
1677 ac->session, rate, channels);
1678
1679 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
1680 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1681 enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
1682 enc_cfg.encdec.param_size = sizeof(enc_cfg) - sizeof(enc_cfg.hdr) -
1683 sizeof(enc_cfg.encdec);
1684 enc_cfg.encblk.frames_per_buf = frames_per_buf;
1685 enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
1686 sizeof(struct asm_enc_cfg_blk_param_v2);
1687
1688 enc_cfg.num_channels = channels;
1689 enc_cfg.bits_per_sample = 16;
1690 enc_cfg.sample_rate = rate;
1691 enc_cfg.is_signed = 1;
1692 channel_mapping = enc_cfg.channel_mapping; /* ??? PHANI */
1693
1694 memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
1695
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001696 if (q6asm_map_channels(channel_mapping, channels))
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001697 return -EINVAL;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001698
1699 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
1700 if (rc < 0) {
1701 pr_err("Comamnd open failed\n");
1702 rc = -EINVAL;
1703 goto fail_cmd;
1704 }
1705 rc = wait_event_timeout(ac->cmd_wait,
1706 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1707 if (!rc) {
1708 pr_err("timeout opcode[0x%x] ", enc_cfg.hdr.opcode);
1709 goto fail_cmd;
1710 }
1711 return 0;
1712fail_cmd:
1713 return -EINVAL;
1714}
1715
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001716int q6asm_enc_cfg_blk_pcm_native(struct audio_client *ac,
1717 uint32_t rate, uint32_t channels)
1718{
1719 struct asm_multi_channel_pcm_enc_cfg_v2 enc_cfg;
1720 u8 *channel_mapping;
1721 u32 frames_per_buf = 0;
1722
1723 int rc = 0;
1724
1725 pr_debug("%s: Session %d, rate = %d, channels = %d\n", __func__,
1726 ac->session, rate, channels);
1727
1728 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
1729
1730 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1731 enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
1732 enc_cfg.encdec.param_size = sizeof(enc_cfg) - sizeof(enc_cfg.hdr) -
1733 sizeof(enc_cfg.encdec);
1734 enc_cfg.encblk.frames_per_buf = frames_per_buf;
1735 enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
1736 sizeof(struct asm_enc_cfg_blk_param_v2);
1737
1738 enc_cfg.num_channels = 0;/*channels;*/
1739 enc_cfg.bits_per_sample = 16;
1740 enc_cfg.sample_rate = 0;/*rate;*/
1741 enc_cfg.is_signed = 1;
1742 channel_mapping = enc_cfg.channel_mapping; /* ??? PHANI */
1743
1744 memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
1745
1746 if (q6asm_map_channels(channel_mapping, channels))
1747 return -EINVAL;
1748
1749 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
1750 if (rc < 0) {
1751 pr_err("Comamnd open failed\n");
1752 rc = -EINVAL;
1753 goto fail_cmd;
1754 }
1755 rc = wait_event_timeout(ac->cmd_wait,
1756 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1757 if (!rc) {
1758 pr_err("timeout opcode[0x%x] ", enc_cfg.hdr.opcode);
1759 goto fail_cmd;
1760 }
1761 return 0;
1762fail_cmd:
1763 return -EINVAL;
1764}
1765
1766static int q6asm_map_channels(u8 *channel_mapping, uint32_t channels)
1767{
1768 u8 *lchannel_mapping;
1769 lchannel_mapping = channel_mapping;
1770 pr_debug("%s channels passed: %d\n", __func__, channels);
1771 if (channels == 1) {
1772 lchannel_mapping[0] = PCM_CHANNEL_FC;
1773 } else if (channels == 2) {
1774 lchannel_mapping[0] = PCM_CHANNEL_FL;
1775 lchannel_mapping[1] = PCM_CHANNEL_FR;
1776 } else if (channels == 3) {
1777 lchannel_mapping[0] = PCM_CHANNEL_FC;
1778 lchannel_mapping[1] = PCM_CHANNEL_FL;
1779 lchannel_mapping[2] = PCM_CHANNEL_FR;
1780 } else if (channels == 4) {
1781 lchannel_mapping[0] = PCM_CHANNEL_FC;
1782 lchannel_mapping[1] = PCM_CHANNEL_FL;
1783 lchannel_mapping[2] = PCM_CHANNEL_FR;
1784 lchannel_mapping[3] = PCM_CHANNEL_LB;
1785 } else if (channels == 5) {
1786 lchannel_mapping[0] = PCM_CHANNEL_FC;
1787 lchannel_mapping[1] = PCM_CHANNEL_FL;
1788 lchannel_mapping[2] = PCM_CHANNEL_FR;
1789 lchannel_mapping[3] = PCM_CHANNEL_LB;
1790 lchannel_mapping[4] = PCM_CHANNEL_RB;
1791 } else if (channels == 6) {
1792 lchannel_mapping[0] = PCM_CHANNEL_FC;
1793 lchannel_mapping[1] = PCM_CHANNEL_FL;
1794 lchannel_mapping[2] = PCM_CHANNEL_FR;
1795 lchannel_mapping[3] = PCM_CHANNEL_LB;
1796 lchannel_mapping[4] = PCM_CHANNEL_RB;
1797 lchannel_mapping[5] = PCM_CHANNEL_LFE;
1798 } else {
1799 pr_err("%s: ERROR.unsupported num_ch = %u\n",
1800 __func__, channels);
1801 return -EINVAL;
1802 }
1803 return 0;
1804}
1805
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001806int q6asm_enable_sbrps(struct audio_client *ac,
1807 uint32_t sbr_ps_enable)
1808{
1809 struct asm_aac_sbr_ps_flag_param sbrps;
1810 u32 frames_per_buf = 0;
1811
1812 int rc = 0;
1813
1814 pr_debug("%s: Session %d\n", __func__, ac->session);
1815
1816 q6asm_add_hdr(ac, &sbrps.hdr, sizeof(sbrps), TRUE);
1817
1818 sbrps.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1819 sbrps.encdec.param_id = ASM_PARAM_ID_AAC_SBR_PS_FLAG;
1820 sbrps.encdec.param_size = sizeof(struct asm_aac_sbr_ps_flag_param) -
1821 sizeof(struct asm_stream_cmd_set_encdec_param);
1822 sbrps.encblk.frames_per_buf = frames_per_buf;
1823 sbrps.encblk.enc_cfg_blk_size = sbrps.encdec.param_size -
1824 sizeof(struct asm_enc_cfg_blk_param_v2);
1825
1826 sbrps.sbr_ps_flag = sbr_ps_enable;
1827
1828 rc = apr_send_pkt(ac->apr, (uint32_t *) &sbrps);
1829 if (rc < 0) {
1830 pr_err("Command opcode[0x%x]paramid[0x%x] failed\n",
1831 ASM_STREAM_CMD_SET_ENCDEC_PARAM,
1832 ASM_PARAM_ID_AAC_SBR_PS_FLAG);
1833 rc = -EINVAL;
1834 goto fail_cmd;
1835 }
1836 rc = wait_event_timeout(ac->cmd_wait,
1837 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1838 if (!rc) {
1839 pr_err("timeout opcode[0x%x] ", sbrps.hdr.opcode);
1840 goto fail_cmd;
1841 }
1842 return 0;
1843fail_cmd:
1844 return -EINVAL;
1845}
1846
1847int q6asm_cfg_dual_mono_aac(struct audio_client *ac,
1848 uint16_t sce_left, uint16_t sce_right)
1849{
1850 struct asm_aac_dual_mono_mapping_param dual_mono;
1851 u32 frames_per_buf = 0;
1852
1853 int rc = 0;
1854
1855 pr_debug("%s: Session %d, sce_left = %d, sce_right = %d\n",
1856 __func__, ac->session, sce_left, sce_right);
1857
1858 q6asm_add_hdr(ac, &dual_mono.hdr, sizeof(dual_mono), TRUE);
1859
1860 dual_mono.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1861 dual_mono.encdec.param_id = ASM_PARAM_ID_AAC_DUAL_MONO_MAPPING;
1862 dual_mono.encdec.param_size = sizeof(struct asm_aac_enc_cfg_v2) -
1863 sizeof(struct asm_stream_cmd_set_encdec_param);
1864 dual_mono.encblk.frames_per_buf = frames_per_buf;
1865 dual_mono.encblk.enc_cfg_blk_size = dual_mono.encdec.param_size -
1866 sizeof(struct asm_enc_cfg_blk_param_v2);
1867 dual_mono.left_channel_sce = sce_left;
1868 dual_mono.right_channel_sce = sce_right;
1869
1870 rc = apr_send_pkt(ac->apr, (uint32_t *) &dual_mono);
1871 if (rc < 0) {
1872 pr_err("%s:Command opcode[0x%x]paramid[0x%x] failed\n",
1873 __func__, ASM_STREAM_CMD_SET_ENCDEC_PARAM,
1874 ASM_PARAM_ID_AAC_DUAL_MONO_MAPPING);
1875 rc = -EINVAL;
1876 goto fail_cmd;
1877 }
1878 rc = wait_event_timeout(ac->cmd_wait,
1879 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1880 if (!rc) {
1881 pr_err("%s:timeout opcode[0x%x]\n", __func__,
1882 dual_mono.hdr.opcode);
1883 goto fail_cmd;
1884 }
1885 return 0;
1886fail_cmd:
1887 return -EINVAL;
1888}
1889
1890int q6asm_enc_cfg_blk_qcelp(struct audio_client *ac, uint32_t frames_per_buf,
1891 uint16_t min_rate, uint16_t max_rate,
1892 uint16_t reduced_rate_level, uint16_t rate_modulation_cmd)
1893{
1894 struct asm_v13k_enc_cfg enc_cfg;
1895 int rc = 0;
1896
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001897 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]",
1898 __func__,
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001899 ac->session, frames_per_buf, min_rate, max_rate,
1900 reduced_rate_level, rate_modulation_cmd);
1901
1902 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
1903 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1904 enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
1905 enc_cfg.encdec.param_size = sizeof(struct asm_v13k_enc_cfg) -
1906 sizeof(struct asm_stream_cmd_set_encdec_param);
1907 enc_cfg.encblk.frames_per_buf = frames_per_buf;
1908 enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
1909 sizeof(struct asm_enc_cfg_blk_param_v2);
1910
1911 enc_cfg.min_rate = min_rate;
1912 enc_cfg.max_rate = max_rate;
1913 enc_cfg.reduced_rate_cmd = reduced_rate_level;
1914 enc_cfg.rate_mod_cmd = rate_modulation_cmd;
1915
1916 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
1917 if (rc < 0) {
1918 pr_err("Comamnd %d failed\n", ASM_STREAM_CMD_SET_ENCDEC_PARAM);
1919 goto fail_cmd;
1920 }
1921 rc = wait_event_timeout(ac->cmd_wait,
1922 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1923 if (!rc) {
1924 pr_err("timeout. waited for setencdec v13k resp\n");
1925 goto fail_cmd;
1926 }
1927 return 0;
1928fail_cmd:
1929 return -EINVAL;
1930}
1931
1932int q6asm_enc_cfg_blk_evrc(struct audio_client *ac, uint32_t frames_per_buf,
1933 uint16_t min_rate, uint16_t max_rate,
1934 uint16_t rate_modulation_cmd)
1935{
1936 struct asm_evrc_enc_cfg enc_cfg;
1937 int rc = 0;
1938
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001939 pr_debug("%s:session[%d]frames[%d]min_rate[0x%4x]max_rate[0x%4x] rate_modulation_cmd[0x%4x]",
1940 __func__, ac->session,
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07001941 frames_per_buf, min_rate, max_rate, rate_modulation_cmd);
1942
1943 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
1944 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1945 enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
1946 enc_cfg.encdec.param_size = sizeof(struct asm_evrc_enc_cfg) -
1947 sizeof(struct asm_stream_cmd_set_encdec_param);
1948 enc_cfg.encblk.frames_per_buf = frames_per_buf;
1949 enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
1950 sizeof(struct asm_enc_cfg_blk_param_v2);
1951
1952 enc_cfg.min_rate = min_rate;
1953 enc_cfg.max_rate = max_rate;
1954 enc_cfg.rate_mod_cmd = rate_modulation_cmd;
1955 enc_cfg.reserved = 0;
1956
1957 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
1958 if (rc < 0) {
1959 pr_err("Comamnd %d failed\n", ASM_STREAM_CMD_SET_ENCDEC_PARAM);
1960 goto fail_cmd;
1961 }
1962 rc = wait_event_timeout(ac->cmd_wait,
1963 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1964 if (!rc) {
1965 pr_err("timeout. waited for encdec evrc\n");
1966 goto fail_cmd;
1967 }
1968 return 0;
1969fail_cmd:
1970 return -EINVAL;
1971}
1972
1973int q6asm_enc_cfg_blk_amrnb(struct audio_client *ac, uint32_t frames_per_buf,
1974 uint16_t band_mode, uint16_t dtx_enable)
1975{
1976 struct asm_amrnb_enc_cfg enc_cfg;
1977 int rc = 0;
1978
1979 pr_debug("%s:session[%d]frames[%d]band_mode[0x%4x]dtx_enable[0x%4x]",
1980 __func__, ac->session, frames_per_buf, band_mode, dtx_enable);
1981
1982 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
1983 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1984 enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
1985 enc_cfg.encdec.param_size = sizeof(struct asm_amrnb_enc_cfg) -
1986 sizeof(struct asm_stream_cmd_set_encdec_param);
1987 enc_cfg.encblk.frames_per_buf = frames_per_buf;
1988 enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
1989 sizeof(struct asm_enc_cfg_blk_param_v2);
1990
1991 enc_cfg.enc_mode = band_mode;
1992 enc_cfg.dtx_mode = dtx_enable;
1993
1994 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
1995 if (rc < 0) {
1996 pr_err("Comamnd %d failed\n", ASM_STREAM_CMD_SET_ENCDEC_PARAM);
1997 goto fail_cmd;
1998 }
1999 rc = wait_event_timeout(ac->cmd_wait,
2000 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2001 if (!rc) {
2002 pr_err("timeout. waited for set encdec amrnb\n");
2003 goto fail_cmd;
2004 }
2005 return 0;
2006fail_cmd:
2007 return -EINVAL;
2008}
2009
2010int q6asm_enc_cfg_blk_amrwb(struct audio_client *ac, uint32_t frames_per_buf,
2011 uint16_t band_mode, uint16_t dtx_enable)
2012{
2013 struct asm_amrwb_enc_cfg enc_cfg;
2014 int rc = 0;
2015
2016 pr_debug("%s:session[%d]frames[%d]band_mode[0x%4x]dtx_enable[0x%4x]",
2017 __func__, ac->session, frames_per_buf, band_mode, dtx_enable);
2018
2019 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
2020 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
2021 enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
2022 enc_cfg.encdec.param_size = sizeof(struct asm_amrwb_enc_cfg) -
2023 sizeof(struct asm_stream_cmd_set_encdec_param);
2024 enc_cfg.encblk.frames_per_buf = frames_per_buf;
2025 enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
2026 sizeof(struct asm_enc_cfg_blk_param_v2);
2027
2028 enc_cfg.enc_mode = band_mode;
2029 enc_cfg.dtx_mode = dtx_enable;
2030
2031 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
2032 if (rc < 0) {
2033 pr_err("Comamnd %d failed\n", ASM_STREAM_CMD_SET_ENCDEC_PARAM);
2034 goto fail_cmd;
2035 }
2036 rc = wait_event_timeout(ac->cmd_wait,
2037 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2038 if (!rc) {
2039 pr_err("timeout. waited for FORMAT_UPDATE\n");
2040 goto fail_cmd;
2041 }
2042 return 0;
2043fail_cmd:
2044 return -EINVAL;
2045}
2046
2047
2048int q6asm_media_format_block_aac(struct audio_client *ac,
2049 struct asm_aac_cfg *cfg)
2050{
2051 return q6asm_media_format_block_multi_aac(ac, cfg);
2052}
2053
2054int q6asm_media_format_block_pcm(struct audio_client *ac,
2055 uint32_t rate, uint32_t channels)
2056{
2057 struct asm_multi_channel_pcm_fmt_blk_v2 fmt;
2058 u8 *channel_mapping;
2059 int rc = 0;
2060
2061 pr_debug("%s:session[%d]rate[%d]ch[%d]\n", __func__, ac->session, rate,
2062 channels);
2063
2064 q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
2065
2066 fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
2067 fmt.fmt_blk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
2068 sizeof(fmt.fmt_blk);
2069 fmt.num_channels = channels;
2070 fmt.bits_per_sample = 16;
2071 fmt.sample_rate = rate;
2072 fmt.is_signed = 1;
2073
2074 channel_mapping = fmt.channel_mapping;
2075
2076 memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
2077
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002078 if (q6asm_map_channels(channel_mapping, channels))
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002079 return -EINVAL;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002080
2081 rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
2082 if (rc < 0) {
2083 pr_err("%s:Comamnd open failed\n", __func__);
2084 goto fail_cmd;
2085 }
2086 rc = wait_event_timeout(ac->cmd_wait,
2087 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2088 if (!rc) {
2089 pr_err("%s:timeout. waited for format update\n", __func__);
2090 goto fail_cmd;
2091 }
2092 return 0;
2093fail_cmd:
2094 return -EINVAL;
2095}
2096
2097int q6asm_media_format_block_multi_aac(struct audio_client *ac,
2098 struct asm_aac_cfg *cfg)
2099{
2100 struct asm_aac_fmt_blk_v2 fmt;
2101 int rc = 0;
2102
2103 pr_debug("%s:session[%d]rate[%d]ch[%d]\n", __func__, ac->session,
2104 cfg->sample_rate, cfg->ch_cfg);
2105
2106 q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
2107
2108 fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
2109 fmt.fmt_blk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
2110 sizeof(fmt.fmt_blk);
2111 fmt.aac_fmt_flag = cfg->format;
2112 fmt.audio_objype = cfg->aot;
2113 /* If zero, PCE is assumed to be available in bitstream*/
2114 fmt.total_size_of_PCE_bits = 0;
2115 fmt.channel_config = cfg->ch_cfg;
2116 fmt.sample_rate = cfg->sample_rate;
2117
2118 pr_info("%s:format=%x cfg_size=%d aac-cfg=%x aot=%d ch=%d sr=%d\n",
2119 __func__, fmt.aac_fmt_flag, fmt.fmt_blk.fmt_blk_size,
2120 fmt.aac_fmt_flag,
2121 fmt.audio_objype,
2122 fmt.channel_config,
2123 fmt.sample_rate);
2124 rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
2125 if (rc < 0) {
2126 pr_err("%s:Comamnd open failed\n", __func__);
2127 goto fail_cmd;
2128 }
2129 rc = wait_event_timeout(ac->cmd_wait,
2130 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2131 if (!rc) {
2132 pr_err("%s:timeout. waited for FORMAT_UPDATE\n", __func__);
2133 goto fail_cmd;
2134 }
2135 return 0;
2136fail_cmd:
2137 return -EINVAL;
2138}
2139
2140int q6asm_media_format_block_wma(struct audio_client *ac,
2141 void *cfg)
2142{
2143 struct asm_wmastdv9_fmt_blk_v2 fmt;
2144 struct asm_wma_cfg *wma_cfg = (struct asm_wma_cfg *)cfg;
2145 int rc = 0;
2146
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002147 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 -07002148 ac->session, wma_cfg->format_tag, wma_cfg->sample_rate,
2149 wma_cfg->ch_cfg, wma_cfg->avg_bytes_per_sec,
2150 wma_cfg->block_align, wma_cfg->valid_bits_per_sample,
2151 wma_cfg->ch_mask, wma_cfg->encode_opt);
2152
2153 q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
2154
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002155 fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
2156 fmt.fmtblk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
2157 sizeof(fmt.fmtblk);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002158 fmt.fmtag = wma_cfg->format_tag;
2159 fmt.num_channels = wma_cfg->ch_cfg;
2160 fmt.sample_rate = wma_cfg->sample_rate;
2161 fmt.avg_bytes_per_sec = wma_cfg->avg_bytes_per_sec;
2162 fmt.blk_align = wma_cfg->block_align;
2163 fmt.bits_per_sample =
2164 wma_cfg->valid_bits_per_sample;
2165 fmt.channel_mask = wma_cfg->ch_mask;
2166 fmt.enc_options = wma_cfg->encode_opt;
2167
2168 rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
2169 if (rc < 0) {
2170 pr_err("%s:Comamnd open failed\n", __func__);
2171 goto fail_cmd;
2172 }
2173 rc = wait_event_timeout(ac->cmd_wait,
2174 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2175 if (!rc) {
2176 pr_err("%s:timeout. waited for FORMAT_UPDATE\n", __func__);
2177 goto fail_cmd;
2178 }
2179 return 0;
2180fail_cmd:
2181 return -EINVAL;
2182}
2183
2184int q6asm_media_format_block_wmapro(struct audio_client *ac,
2185 void *cfg)
2186{
2187 struct asm_wmaprov10_fmt_blk_v2 fmt;
2188 struct asm_wmapro_cfg *wmapro_cfg = (struct asm_wmapro_cfg *)cfg;
2189 int rc = 0;
2190
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002191 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 -07002192 ac->session, wmapro_cfg->format_tag, wmapro_cfg->sample_rate,
2193 wmapro_cfg->ch_cfg, wmapro_cfg->avg_bytes_per_sec,
2194 wmapro_cfg->block_align, wmapro_cfg->valid_bits_per_sample,
2195 wmapro_cfg->ch_mask, wmapro_cfg->encode_opt,
2196 wmapro_cfg->adv_encode_opt, wmapro_cfg->adv_encode_opt2);
2197
2198 q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
2199
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002200 fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
2201 fmt.fmtblk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
2202 sizeof(fmt.fmtblk);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002203
2204 fmt.fmtag = wmapro_cfg->format_tag;
2205 fmt.num_channels = wmapro_cfg->ch_cfg;
2206 fmt.sample_rate = wmapro_cfg->sample_rate;
2207 fmt.avg_bytes_per_sec =
2208 wmapro_cfg->avg_bytes_per_sec;
2209 fmt.blk_align = wmapro_cfg->block_align;
2210 fmt.bits_per_sample = wmapro_cfg->valid_bits_per_sample;
2211 fmt.channel_mask = wmapro_cfg->ch_mask;
2212 fmt.enc_options = wmapro_cfg->encode_opt;
2213 fmt.usAdvancedEncodeOpt = wmapro_cfg->adv_encode_opt;
2214 fmt.advanced_enc_options2 = wmapro_cfg->adv_encode_opt2;
2215
2216 rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
2217 if (rc < 0) {
2218 pr_err("%s:Comamnd open failed\n", __func__);
2219 goto fail_cmd;
2220 }
2221 rc = wait_event_timeout(ac->cmd_wait,
2222 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2223 if (!rc) {
2224 pr_err("%s:timeout. waited for FORMAT_UPDATE\n", __func__);
2225 goto fail_cmd;
2226 }
2227 return 0;
2228fail_cmd:
2229 return -EINVAL;
2230}
2231
2232int q6asm_memory_map(struct audio_client *ac, uint32_t buf_add, int dir,
2233 uint32_t bufsz, uint32_t bufcnt)
2234{
2235 struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
2236 struct avs_shared_map_region_payload *mregions = NULL;
2237 struct audio_port_data *port = NULL;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002238 void *mmap_region_cmd = NULL;
2239 void *payload = NULL;
2240 struct asm_buffer_node *buffer_node = NULL;
2241 int rc = 0;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002242 int cmd_size = 0;
2243
2244 if (!ac || ac->apr == NULL || ac->mmap_apr == NULL) {
2245 pr_err("APR handle NULL\n");
2246 return -EINVAL;
2247 }
2248 pr_debug("%s: Session[%d]\n", __func__, ac->session);
2249
2250 buffer_node = kmalloc(sizeof(struct asm_buffer_node), GFP_KERNEL);
2251 if (!buffer_node)
2252 return -ENOMEM;
2253 cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
2254 + sizeof(struct avs_shared_map_region_payload) * bufcnt;
2255
2256 mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
2257 if (mmap_region_cmd == NULL) {
2258 pr_err("%s: Mem alloc failed\n", __func__);
2259 rc = -EINVAL;
2260 return rc;
2261 }
2262 mmap_regions = (struct avs_cmd_shared_mem_map_regions *)
2263 mmap_region_cmd;
2264 q6asm_add_mmaphdr(ac, &mmap_regions->hdr, cmd_size,
2265 TRUE, ((ac->session << 8) | dir));
2266 mmap_regions->hdr.opcode = ASM_CMD_SHARED_MEM_MAP_REGIONS;
Harmandeep Singhac1671b2012-06-22 15:34:45 -07002267 mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002268 mmap_regions->num_regions = bufcnt & 0x00ff;
2269 mmap_regions->property_flag = 0x00;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002270 payload = ((u8 *) mmap_region_cmd +
2271 sizeof(struct avs_cmd_shared_mem_map_regions));
2272 mregions = (struct avs_shared_map_region_payload *)payload;
2273
2274 ac->port[dir].tmp_hdl = 0;
2275 port = &ac->port[dir];
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002276 pr_debug("%s, buf_add 0x%x, bufsz: %d\n", __func__, buf_add, bufsz);
2277 mregions->shm_addr_lsw = buf_add;
2278 /* Using only 32 bit address */
2279 mregions->shm_addr_msw = 0;
2280 mregions->mem_size_bytes = bufsz;
2281 ++mregions;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002282
2283 rc = apr_send_pkt(ac->mmap_apr, (uint32_t *) mmap_region_cmd);
2284 if (rc < 0) {
2285 pr_err("mmap op[0x%x]rc[%d]\n",
2286 mmap_regions->hdr.opcode, rc);
2287 rc = -EINVAL;
2288 goto fail_cmd;
2289 }
2290
2291 rc = wait_event_timeout(ac->cmd_wait,
2292 (atomic_read(&ac->cmd_state) == 0 &&
2293 ac->port[dir].tmp_hdl), 5*HZ);
2294 if (!rc) {
2295 pr_err("timeout. waited for memory_map\n");
2296 rc = -EINVAL;
2297 goto fail_cmd;
2298 }
2299 buffer_node->buf_addr_lsw = buf_add;
2300 buffer_node->mmap_hdl = ac->port[dir].tmp_hdl;
2301 list_add_tail(&buffer_node->list, &ac->port[dir].mem_map_handle);
2302 ac->port[dir].tmp_hdl = 0;
2303 rc = 0;
2304
2305fail_cmd:
2306 kfree(mmap_region_cmd);
2307 return rc;
2308}
2309
2310int q6asm_memory_unmap(struct audio_client *ac, uint32_t buf_add, int dir)
2311{
2312 struct avs_cmd_shared_mem_unmap_regions mem_unmap;
2313 struct asm_buffer_node *buf_node = NULL;
2314 struct list_head *ptr, *next;
2315
2316 int rc = 0;
2317
2318 if (!ac || ac->apr == NULL || this_mmap.apr == NULL) {
2319 pr_err("APR handle NULL\n");
2320 return -EINVAL;
2321 }
2322 pr_debug("%s: Session[%d]\n", __func__, ac->session);
2323
2324 q6asm_add_mmaphdr(ac, &mem_unmap.hdr,
2325 sizeof(struct avs_cmd_shared_mem_unmap_regions),
2326 TRUE, ((ac->session << 8) | dir));
2327
2328 mem_unmap.hdr.opcode = ASM_CMD_SHARED_MEM_UNMAP_REGIONS;
2329 list_for_each_safe(ptr, next, &ac->port[dir].mem_map_handle) {
2330 buf_node = list_entry(ptr, struct asm_buffer_node,
2331 list);
2332 if (buf_node->buf_addr_lsw == buf_add) {
2333 pr_info("%s: Found the element\n", __func__);
2334 mem_unmap.mem_map_handle = buf_node->mmap_hdl;
2335 break;
2336 }
2337 }
2338 pr_debug("%s: mem_unmap-mem_map_handle: 0x%x",
2339 __func__, mem_unmap.mem_map_handle);
2340 rc = apr_send_pkt(ac->mmap_apr, (uint32_t *) &mem_unmap);
2341 if (rc < 0) {
2342 pr_err("mem_unmap op[0x%x]rc[%d]\n",
2343 mem_unmap.hdr.opcode, rc);
2344 rc = -EINVAL;
2345 goto fail_cmd;
2346 }
2347
2348 rc = wait_event_timeout(ac->cmd_wait,
2349 (atomic_read(&ac->cmd_state) == 0), 5 * HZ);
2350 if (!rc) {
2351 pr_err("timeout. waited for memory_map\n");
2352 rc = -EINVAL;
2353 goto fail_cmd;
2354 }
2355 list_for_each_safe(ptr, next, &ac->port[dir].mem_map_handle) {
2356 buf_node = list_entry(ptr, struct asm_buffer_node,
2357 list);
2358 if (buf_node->buf_addr_lsw == buf_add) {
2359 list_del(&buf_node->list);
2360 kfree(buf_node);
2361 }
2362 }
2363
2364 rc = 0;
2365fail_cmd:
2366 return rc;
2367}
2368
2369
2370static int q6asm_memory_map_regions(struct audio_client *ac, int dir,
2371 uint32_t bufsz, uint32_t bufcnt)
2372{
2373 struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
2374 struct avs_shared_map_region_payload *mregions = NULL;
2375 struct audio_port_data *port = NULL;
2376 struct audio_buffer *ab = NULL;
2377 void *mmap_region_cmd = NULL;
2378 void *payload = NULL;
2379 struct asm_buffer_node *buffer_node = NULL;
2380 int rc = 0;
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002381 int i = 0;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002382 int cmd_size = 0;
2383
2384 if (!ac || ac->apr == NULL || ac->mmap_apr == NULL) {
2385 pr_err("APR handle NULL\n");
2386 return -EINVAL;
2387 }
2388 pr_debug("%s: Session[%d]\n", __func__, ac->session);
2389
2390 cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
2391 + (sizeof(struct avs_shared_map_region_payload));
2392
2393 buffer_node = kzalloc(sizeof(struct asm_buffer_node) * bufcnt,
2394 GFP_KERNEL);
2395
2396 mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
2397 if ((mmap_region_cmd == NULL) || (buffer_node == NULL)) {
2398 pr_err("%s: Mem alloc failed\n", __func__);
2399 rc = -EINVAL;
2400 return rc;
2401 }
2402 mmap_regions = (struct avs_cmd_shared_mem_map_regions *)
2403 mmap_region_cmd;
2404 q6asm_add_mmaphdr(ac, &mmap_regions->hdr, cmd_size, TRUE,
2405 ((ac->session << 8) | dir));
2406 pr_debug("mmap_region=0x%p token=0x%x\n",
2407 mmap_regions, ((ac->session << 8) | dir));
2408
2409 mmap_regions->hdr.opcode = ASM_CMD_SHARED_MEM_MAP_REGIONS;
Harmandeep Singhac1671b2012-06-22 15:34:45 -07002410 mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002411 mmap_regions->num_regions = 1; /*bufcnt & 0x00ff; */
2412 mmap_regions->property_flag = 0x00;
2413 pr_debug("map_regions->nregions = %d\n", mmap_regions->num_regions);
2414 payload = ((u8 *) mmap_region_cmd +
2415 sizeof(struct avs_cmd_shared_mem_map_regions));
2416 mregions = (struct avs_shared_map_region_payload *)payload;
2417
2418 ac->port[dir].tmp_hdl = 0;
2419 port = &ac->port[dir];
2420 ab = &port->buf[0];
2421 mregions->shm_addr_lsw = ab->phys;
2422 /* Using only 32 bit address */
2423 mregions->shm_addr_msw = 0;
2424 mregions->mem_size_bytes = (bufsz * bufcnt);
2425
2426 rc = apr_send_pkt(ac->mmap_apr, (uint32_t *) mmap_region_cmd);
2427 if (rc < 0) {
2428 pr_err("mmap_regions op[0x%x]rc[%d]\n",
2429 mmap_regions->hdr.opcode, rc);
2430 rc = -EINVAL;
2431 goto fail_cmd;
2432 }
2433
2434 rc = wait_event_timeout(ac->cmd_wait,
2435 (atomic_read(&ac->cmd_state) == 0)
2436 , 5*HZ);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002437 if (!rc) {
2438 pr_err("timeout. waited for memory_map\n");
2439 rc = -EINVAL;
2440 goto fail_cmd;
2441 }
2442 mutex_lock(&ac->cmd_lock);
2443
2444 for (i = 0; i < bufcnt; i++) {
2445 ab = &port->buf[i];
2446 buffer_node[i].buf_addr_lsw = ab->phys;
2447 buffer_node[i].mmap_hdl = ac->port[dir].tmp_hdl;
2448 list_add_tail(&buffer_node[i].list,
2449 &ac->port[dir].mem_map_handle);
2450 pr_debug("%s: i=%d, bufadd[i] = 0x%x, maphdl[i] = 0x%x\n",
2451 __func__, i, buffer_node[i].buf_addr_lsw,
2452 buffer_node[i].mmap_hdl);
2453 }
2454 ac->port[dir].tmp_hdl = 0;
2455 mutex_unlock(&ac->cmd_lock);
2456 rc = 0;
2457 pr_debug("%s: exit\n", __func__);
2458fail_cmd:
2459 kfree(mmap_region_cmd);
2460 return rc;
2461}
2462
2463static int q6asm_memory_unmap_regions(struct audio_client *ac, int dir,
2464 uint32_t bufsz, uint32_t bufcnt)
2465{
2466 struct avs_cmd_shared_mem_unmap_regions mem_unmap;
2467 struct audio_port_data *port = NULL;
2468 struct asm_buffer_node *buf_node = NULL;
2469 struct list_head *ptr, *next;
2470 uint32_t buf_add;
2471 int rc = 0;
2472 int cmd_size = 0;
2473
2474 if (!ac || ac->apr == NULL || ac->mmap_apr == NULL) {
2475 pr_err("APR handle NULL\n");
2476 return -EINVAL;
2477 }
2478 pr_debug("%s: Session[%d]\n", __func__, ac->session);
2479
2480 cmd_size = sizeof(struct avs_cmd_shared_mem_unmap_regions);
2481 q6asm_add_mmaphdr(ac, &mem_unmap.hdr, cmd_size,
2482 TRUE, ((ac->session << 8) | dir));
2483 port = &ac->port[dir];
2484 buf_add = (uint32_t)port->buf->phys;
2485 mem_unmap.hdr.opcode = ASM_CMD_SHARED_MEM_UNMAP_REGIONS;
2486 list_for_each_safe(ptr, next, &ac->port[dir].mem_map_handle) {
2487 buf_node = list_entry(ptr, struct asm_buffer_node,
2488 list);
2489 if (buf_node->buf_addr_lsw == buf_add) {
2490 pr_debug("%s: Found the element\n", __func__);
2491 mem_unmap.mem_map_handle = buf_node->mmap_hdl;
2492 break;
2493 }
2494 }
2495
2496 pr_debug("%s: mem_unmap-mem_map_handle: 0x%x",
2497 __func__, mem_unmap.mem_map_handle);
2498 rc = apr_send_pkt(ac->mmap_apr, (uint32_t *) &mem_unmap);
2499 if (rc < 0) {
2500 pr_err("mmap_regions op[0x%x]rc[%d]\n",
2501 mem_unmap.hdr.opcode, rc);
2502 goto fail_cmd;
2503 }
2504
2505 rc = wait_event_timeout(ac->cmd_wait,
2506 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2507 if (!rc) {
2508 pr_err("timeout. waited for memory_unmap\n");
2509 goto fail_cmd;
2510 }
2511 list_for_each_safe(ptr, next, &ac->port[dir].mem_map_handle) {
2512 buf_node = list_entry(ptr, struct asm_buffer_node,
2513 list);
2514 if (buf_node->buf_addr_lsw == buf_add) {
2515 list_del(&buf_node->list);
2516 kfree(buf_node);
2517 }
2518 }
2519 rc = 0;
2520
2521fail_cmd:
2522 return rc;
2523}
2524
2525int q6asm_set_lrgain(struct audio_client *ac, int left_gain, int right_gain)
2526{
2527 struct asm_volume_ctrl_lr_chan_gain lrgain;
2528 int sz = 0;
2529 int rc = 0;
2530
2531 sz = sizeof(struct asm_volume_ctrl_lr_chan_gain);
2532 q6asm_add_hdr_async(ac, &lrgain.hdr, sz, TRUE);
2533 lrgain.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
2534 lrgain.param.data_payload_addr_lsw = 0;
2535 lrgain.param.data_payload_addr_msw = 0;
2536 lrgain.param.mem_map_handle = 0;
2537 lrgain.param.data_payload_size = sizeof(lrgain) -
2538 sizeof(lrgain.hdr) - sizeof(lrgain.param);
2539 lrgain.data.module_id = ASM_MODULE_ID_VOL_CTRL;
2540 lrgain.data.param_id = ASM_PARAM_ID_VOL_CTRL_LR_CHANNEL_GAIN;
2541 lrgain.data.param_size = lrgain.param.data_payload_size -
2542 sizeof(lrgain.data);
2543 lrgain.data.reserved = 0;
2544 lrgain.l_chan_gain = left_gain;
2545 lrgain.r_chan_gain = right_gain;
2546 rc = apr_send_pkt(ac->apr, (uint32_t *) &lrgain);
2547 if (rc < 0) {
2548 pr_err("%s: set-params send failed paramid[0x%x]\n", __func__,
2549 lrgain.data.param_id);
2550 rc = -EINVAL;
2551 goto fail_cmd;
2552 }
2553
2554 rc = wait_event_timeout(ac->cmd_wait,
2555 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2556 if (!rc) {
2557 pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
2558 lrgain.data.param_id);
2559 rc = -EINVAL;
2560 goto fail_cmd;
2561 }
2562 rc = 0;
2563fail_cmd:
2564 return rc;
2565}
2566
2567int q6asm_set_mute(struct audio_client *ac, int muteflag)
2568{
2569 struct asm_volume_ctrl_mute_config mute;
2570 int sz = 0;
2571 int rc = 0;
2572
2573 sz = sizeof(struct asm_volume_ctrl_mute_config);
2574 q6asm_add_hdr_async(ac, &mute.hdr, sz, TRUE);
2575 mute.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
2576 mute.param.data_payload_addr_lsw = 0;
2577 mute.param.data_payload_addr_msw = 0;
2578 mute.param.mem_map_handle = 0;
2579 mute.param.data_payload_size = sizeof(mute) -
2580 sizeof(mute.hdr) - sizeof(mute.param);
2581 mute.data.module_id = ASM_MODULE_ID_VOL_CTRL;
2582 mute.data.param_id = ASM_PARAM_ID_VOL_CTRL_MUTE_CONFIG;
2583 mute.data.param_size = mute.param.data_payload_size - sizeof(mute.data);
2584 mute.data.reserved = 0;
2585 mute.mute_flag = muteflag;
2586
2587 rc = apr_send_pkt(ac->apr, (uint32_t *) &mute);
2588 if (rc < 0) {
2589 pr_err("%s: set-params send failed paramid[0x%x]\n", __func__,
2590 mute.data.param_id);
2591 rc = -EINVAL;
2592 goto fail_cmd;
2593 }
2594
2595 rc = wait_event_timeout(ac->cmd_wait,
2596 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2597 if (!rc) {
2598 pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
2599 mute.data.param_id);
2600 rc = -EINVAL;
2601 goto fail_cmd;
2602 }
2603 rc = 0;
2604fail_cmd:
2605 return rc;
2606}
2607
2608int q6asm_set_volume(struct audio_client *ac, int volume)
2609{
2610 struct asm_volume_ctrl_master_gain vol;
2611 int sz = 0;
2612 int rc = 0;
2613
2614 sz = sizeof(struct asm_volume_ctrl_master_gain);
2615 q6asm_add_hdr_async(ac, &vol.hdr, sz, TRUE);
2616 vol.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
2617 vol.param.data_payload_addr_lsw = 0;
2618 vol.param.data_payload_addr_msw = 0;
2619
2620
2621 vol.param.mem_map_handle = 0;
2622 vol.param.data_payload_size = sizeof(vol) -
2623 sizeof(vol.hdr) - sizeof(vol.param);
2624 vol.data.module_id = ASM_MODULE_ID_VOL_CTRL;
2625 vol.data.param_id = ASM_PARAM_ID_VOL_CTRL_MASTER_GAIN;
2626 vol.data.param_size = vol.param.data_payload_size - sizeof(vol.data);
2627 vol.data.reserved = 0;
2628 vol.master_gain = volume;
2629
2630 rc = apr_send_pkt(ac->apr, (uint32_t *) &vol);
2631 if (rc < 0) {
2632 pr_err("%s: set-params send failed paramid[0x%x]\n", __func__,
2633 vol.data.param_id);
2634 rc = -EINVAL;
2635 goto fail_cmd;
2636 }
2637
2638 rc = wait_event_timeout(ac->cmd_wait,
2639 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2640 if (!rc) {
2641 pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
2642 vol.data.param_id);
2643 rc = -EINVAL;
2644 goto fail_cmd;
2645 }
2646 rc = 0;
2647fail_cmd:
2648 return rc;
2649}
2650int q6asm_set_softpause(struct audio_client *ac,
2651 struct asm_softpause_params *pause_param)
2652{
2653 struct asm_soft_pause_params softpause;
2654 int sz = 0;
2655 int rc = 0;
2656
2657 sz = sizeof(struct asm_soft_pause_params);
2658 q6asm_add_hdr_async(ac, &softpause.hdr, sz, TRUE);
2659 softpause.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
2660
2661 softpause.param.data_payload_addr_lsw = 0;
2662 softpause.param.data_payload_addr_msw = 0;
2663 softpause.param.mem_map_handle = 0;
2664 softpause.param.data_payload_size = sizeof(softpause) -
2665 sizeof(softpause.hdr) - sizeof(softpause.param);
2666 softpause.data.module_id = ASM_MODULE_ID_VOL_CTRL;
2667 softpause.data.param_id = ASM_PARAM_ID_SOFT_PAUSE_PARAMETERS;
2668 softpause.data.param_size = softpause.param.data_payload_size -
2669 sizeof(softpause.data);
2670 softpause.data.reserved = 0;
2671 softpause.enable_flag = pause_param->enable;
2672 softpause.period = pause_param->period;
2673 softpause.step = pause_param->step;
2674 softpause.ramping_curve = pause_param->rampingcurve;
2675
2676 rc = apr_send_pkt(ac->apr, (uint32_t *) &softpause);
2677 if (rc < 0) {
2678 pr_err("%s: set-params send failed paramid[0x%x]\n", __func__,
2679 softpause.data.param_id);
2680 rc = -EINVAL;
2681 goto fail_cmd;
2682 }
2683
2684 rc = wait_event_timeout(ac->cmd_wait,
2685 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2686 if (!rc) {
2687 pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
2688 softpause.data.param_id);
2689 rc = -EINVAL;
2690 goto fail_cmd;
2691 }
2692 rc = 0;
2693fail_cmd:
2694 return rc;
2695}
2696
2697int q6asm_set_softvolume(struct audio_client *ac,
2698 struct asm_softvolume_params *softvol_param)
2699{
2700 struct asm_soft_step_volume_params softvol;
2701 int sz = 0;
2702 int rc = 0;
2703
2704 sz = sizeof(struct asm_soft_step_volume_params);
2705 q6asm_add_hdr_async(ac, &softvol.hdr, sz, TRUE);
2706 softvol.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
2707 softvol.param.data_payload_addr_lsw = 0;
2708 softvol.param.data_payload_addr_msw = 0;
2709 softvol.param.mem_map_handle = 0;
2710 softvol.param.data_payload_size = sizeof(softvol) -
2711 sizeof(softvol.hdr) - sizeof(softvol.param);
2712 softvol.data.module_id = ASM_MODULE_ID_VOL_CTRL;
2713 softvol.data.param_id = ASM_PARAM_ID_SOFT_VOL_STEPPING_PARAMETERS;
2714 softvol.data.param_size = softvol.param.data_payload_size -
2715 sizeof(softvol.data);
2716 softvol.data.reserved = 0;
2717 softvol.period = softvol_param->period;
2718 softvol.step = softvol_param->step;
2719 softvol.ramping_curve = softvol_param->rampingcurve;
2720
2721 rc = apr_send_pkt(ac->apr, (uint32_t *) &softvol);
2722 if (rc < 0) {
2723 pr_err("%s: set-params send failed paramid[0x%x]\n", __func__,
2724 softvol.data.param_id);
2725 rc = -EINVAL;
2726 goto fail_cmd;
2727 }
2728
2729 rc = wait_event_timeout(ac->cmd_wait,
2730 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2731 if (!rc) {
2732 pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
2733 softvol.data.param_id);
2734 rc = -EINVAL;
2735 goto fail_cmd;
2736 }
2737 rc = 0;
2738fail_cmd:
2739 return rc;
2740}
2741
2742int q6asm_equalizer(struct audio_client *ac, void *eq_p)
2743{
2744 struct asm_eq_params eq;
2745 struct msm_audio_eq_stream_config *eq_params = NULL;
2746 int i = 0;
2747 int sz = 0;
2748 int rc = 0;
2749
2750 if (eq_p == NULL) {
2751 pr_err("%s[%d]: Invalid Eq param\n", __func__, ac->session);
2752 rc = -EINVAL;
2753 goto fail_cmd;
2754 }
2755 sz = sizeof(struct asm_eq_params);
2756 eq_params = (struct msm_audio_eq_stream_config *) eq_p;
2757 q6asm_add_hdr(ac, &eq.hdr, sz, TRUE);
2758
2759 eq.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
2760 eq.param.data_payload_addr_lsw = 0;
2761 eq.param.data_payload_addr_msw = 0;
2762 eq.param.mem_map_handle = 0;
2763 eq.param.data_payload_size = sizeof(eq) -
2764 sizeof(eq.hdr) - sizeof(eq.param);
2765 eq.data.module_id = ASM_MODULE_ID_EQUALIZER;
2766 eq.data.param_id = ASM_PARAM_ID_EQUALIZER_PARAMETERS;
2767 eq.data.param_size = eq.param.data_payload_size - sizeof(eq.data);
2768 eq.enable_flag = eq_params->enable;
2769 eq.num_bands = eq_params->num_bands;
2770
2771 pr_debug("%s: enable:%d numbands:%d\n", __func__, eq_params->enable,
2772 eq_params->num_bands);
2773 for (i = 0; i < eq_params->num_bands; i++) {
2774 eq.eq_bands[i].band_idx =
2775 eq_params->eq_bands[i].band_idx;
2776 eq.eq_bands[i].filterype =
2777 eq_params->eq_bands[i].filter_type;
2778 eq.eq_bands[i].center_freq_hz =
2779 eq_params->eq_bands[i].center_freq_hz;
2780 eq.eq_bands[i].filter_gain =
2781 eq_params->eq_bands[i].filter_gain;
2782 eq.eq_bands[i].q_factor =
2783 eq_params->eq_bands[i].q_factor;
2784 pr_debug("%s: filter_type:%u bandnum:%d\n", __func__,
2785 eq_params->eq_bands[i].filter_type, i);
2786 pr_debug("%s: center_freq_hz:%u bandnum:%d\n", __func__,
2787 eq_params->eq_bands[i].center_freq_hz, i);
2788 pr_debug("%s: filter_gain:%d bandnum:%d\n", __func__,
2789 eq_params->eq_bands[i].filter_gain, i);
2790 pr_debug("%s: q_factor:%d bandnum:%d\n", __func__,
2791 eq_params->eq_bands[i].q_factor, i);
2792 }
2793 rc = apr_send_pkt(ac->apr, (uint32_t *)&eq);
2794 if (rc < 0) {
2795 pr_err("%s: set-params send failed paramid[0x%x]\n", __func__,
2796 eq.data.param_id);
2797 rc = -EINVAL;
2798 goto fail_cmd;
2799 }
2800
2801 rc = wait_event_timeout(ac->cmd_wait,
2802 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2803 if (!rc) {
2804 pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
2805 eq.data.param_id);
2806 rc = -EINVAL;
2807 goto fail_cmd;
2808 }
2809 rc = 0;
2810fail_cmd:
2811 return rc;
2812}
2813
2814int q6asm_read(struct audio_client *ac)
2815{
2816 struct asm_data_cmd_read_v2 read;
2817 struct asm_buffer_node *buf_node = NULL;
2818 struct list_head *ptr, *next;
2819 struct audio_buffer *ab;
2820 int dsp_buf;
2821 struct audio_port_data *port;
2822 int rc;
2823 if (!ac || ac->apr == NULL) {
2824 pr_err("APR handle NULL\n");
2825 return -EINVAL;
2826 }
2827 if (ac->io_mode == SYNC_IO_MODE) {
2828 port = &ac->port[OUT];
2829
2830 q6asm_add_hdr(ac, &read.hdr, sizeof(read), FALSE);
2831
2832 mutex_lock(&port->lock);
2833
2834 dsp_buf = port->dsp_buf;
2835 ab = &port->buf[dsp_buf];
2836
2837 pr_debug("%s:session[%d]dsp-buf[%d][%p]cpu_buf[%d][%p]\n",
2838 __func__,
2839 ac->session,
2840 dsp_buf,
2841 (void *)port->buf[dsp_buf].data,
2842 port->cpu_buf,
2843 (void *)port->buf[port->cpu_buf].phys);
2844
2845 read.hdr.opcode = ASM_DATA_CMD_READ_V2;
2846 read.buf_addr_lsw = ab->phys;
2847 read.buf_addr_msw = 0;
2848
2849 list_for_each_safe(ptr, next, &ac->port[OUT].mem_map_handle) {
2850 buf_node = list_entry(ptr, struct asm_buffer_node,
2851 list);
2852 if (buf_node->buf_addr_lsw == (uint32_t) ab->phys)
2853 read.mem_map_handle = buf_node->mmap_hdl;
2854 }
2855 pr_debug("memory_map handle in q6asm_read: [%0x]:",
2856 read.mem_map_handle);
2857 read.buf_size = ab->size;
2858 read.seq_id = port->dsp_buf;
2859 read.hdr.token = port->dsp_buf;
2860 port->dsp_buf = (port->dsp_buf + 1) & (port->max_buf_cnt - 1);
2861 mutex_unlock(&port->lock);
2862 pr_debug("%s:buf add[0x%x] token[%d] uid[%d]\n", __func__,
2863 read.buf_addr_lsw,
2864 read.hdr.token,
2865 read.seq_id);
2866 rc = apr_send_pkt(ac->apr, (uint32_t *) &read);
2867 if (rc < 0) {
2868 pr_err("read op[0x%x]rc[%d]\n", read.hdr.opcode, rc);
2869 goto fail_cmd;
2870 }
2871 return 0;
2872 }
2873fail_cmd:
2874 return -EINVAL;
2875}
2876
2877int q6asm_read_nolock(struct audio_client *ac)
2878{
2879 struct asm_data_cmd_read_v2 read;
2880 struct asm_buffer_node *buf_node = NULL;
2881 struct list_head *ptr, *next;
2882 struct audio_buffer *ab;
2883 int dsp_buf;
2884 struct audio_port_data *port;
2885 int rc;
2886 if (!ac || ac->apr == NULL) {
2887 pr_err("APR handle NULL\n");
2888 return -EINVAL;
2889 }
2890 if (ac->io_mode == SYNC_IO_MODE) {
2891 port = &ac->port[OUT];
2892
2893 q6asm_add_hdr_async(ac, &read.hdr, sizeof(read), FALSE);
2894
2895
2896 dsp_buf = port->dsp_buf;
2897 ab = &port->buf[dsp_buf];
2898
2899 pr_debug("%s:session[%d]dsp-buf[%d][%p]cpu_buf[%d][%p]\n",
2900 __func__,
2901 ac->session,
2902 dsp_buf,
2903 (void *)port->buf[dsp_buf].data,
2904 port->cpu_buf,
2905 (void *)port->buf[port->cpu_buf].phys);
2906
2907 read.hdr.opcode = ASM_DATA_CMD_READ_V2;
2908 read.buf_addr_lsw = ab->phys;
2909 read.buf_addr_msw = 0;
2910 read.buf_size = ab->size;
2911 read.seq_id = port->dsp_buf;
2912 read.hdr.token = port->dsp_buf;
2913
2914 list_for_each_safe(ptr, next, &ac->port[OUT].mem_map_handle) {
2915 buf_node = list_entry(ptr, struct asm_buffer_node,
2916 list);
2917 if (buf_node->buf_addr_lsw == (uint32_t)ab->phys) {
2918 read.mem_map_handle = buf_node->mmap_hdl;
2919 break;
2920 }
2921 }
2922
2923 port->dsp_buf = (port->dsp_buf + 1) & (port->max_buf_cnt - 1);
2924 pr_debug("%s:buf add[0x%x] token[%d] uid[%d]\n", __func__,
2925 read.buf_addr_lsw,
2926 read.hdr.token,
2927 read.seq_id);
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002928 rc = apr_send_pkt(ac->apr, (uint32_t *) &read);
2929 if (rc < 0) {
2930 pr_err("read op[0x%x]rc[%d]\n", read.hdr.opcode, rc);
2931 goto fail_cmd;
2932 }
2933 return 0;
2934 }
2935fail_cmd:
2936 return -EINVAL;
2937}
2938
2939int q6asm_async_write(struct audio_client *ac,
2940 struct audio_aio_write_param *param)
2941{
2942 int rc = 0;
2943 struct asm_data_cmd_write_v2 write;
2944 struct asm_buffer_node *buf_node = NULL;
2945 struct list_head *ptr, *next;
2946 struct audio_buffer *ab;
2947 struct audio_port_data *port;
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002948 u32 lbuf_addr_lsw;
2949 u32 liomode;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002950
2951 if (!ac || ac->apr == NULL) {
2952 pr_err("%s: APR handle NULL\n", __func__);
2953 return -EINVAL;
2954 }
2955
2956 q6asm_add_hdr_async(ac, &write.hdr, sizeof(write), FALSE);
2957
2958 port = &ac->port[IN];
2959 ab = &port->buf[port->dsp_buf];
2960
2961 /* Pass physical address as token for AIO scheme */
2962 write.hdr.token = param->uid;
2963 write.hdr.opcode = ASM_DATA_CMD_WRITE_V2;
2964 write.buf_addr_lsw = param->paddr;
2965 write.buf_addr_msw = 0x00;
2966 write.buf_size = param->len;
2967 write.timestamp_msw = param->msw_ts;
2968 write.timestamp_lsw = param->lsw_ts;
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002969 liomode = (ASYNC_IO_MODE | NT_MODE);
2970
2971 if (ac->io_mode == liomode) {
2972 pr_info("%s: subtracting 32 for header\n", __func__);
2973 lbuf_addr_lsw = (write.buf_addr_lsw - 32);
2974 } else{
2975 lbuf_addr_lsw = write.buf_addr_lsw;
2976 }
2977
2978 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",
2979 __func__,
2980 write.hdr.token, write.buf_addr_lsw,
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002981 write.buf_size, write.timestamp_msw,
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002982 write.timestamp_lsw, lbuf_addr_lsw);
2983
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002984 /* Use 0xFF00 for disabling timestamps */
2985 if (param->flags == 0xFF00)
2986 write.flags = (0x00000000 | (param->flags & 0x800000FF));
2987 else
2988 write.flags = (0x80000000 | param->flags);
2989
2990 write.seq_id = param->uid;
2991 list_for_each_safe(ptr, next, &ac->port[IN].mem_map_handle) {
2992 buf_node = list_entry(ptr, struct asm_buffer_node,
2993 list);
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002994 if (buf_node->buf_addr_lsw == lbuf_addr_lsw) {
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002995 write.mem_map_handle = buf_node->mmap_hdl;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07002996 break;
2997 }
2998 }
2999
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07003000 rc = apr_send_pkt(ac->apr, (uint32_t *) &write);
3001 if (rc < 0) {
3002 pr_debug("[%s] write op[0x%x]rc[%d]\n", __func__,
3003 write.hdr.opcode, rc);
3004 goto fail_cmd;
3005 }
3006 return 0;
3007fail_cmd:
3008 return -EINVAL;
3009}
3010
3011int q6asm_async_read(struct audio_client *ac,
3012 struct audio_aio_read_param *param)
3013{
3014 int rc = 0;
3015 struct asm_data_cmd_read_v2 read;
3016 struct asm_buffer_node *buf_node = NULL;
3017 struct list_head *ptr, *next;
Harmandeep Singheaf59b42012-06-05 21:46:02 -07003018 u32 lbuf_addr_lsw;
3019 u32 liomode;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07003020
3021 if (!ac || ac->apr == NULL) {
3022 pr_err("%s: APR handle NULL\n", __func__);
3023 return -EINVAL;
3024 }
3025
3026 q6asm_add_hdr_async(ac, &read.hdr, sizeof(read), FALSE);
3027
3028 /* Pass physical address as token for AIO scheme */
3029 read.hdr.token = param->paddr;
3030 read.hdr.opcode = ASM_DATA_CMD_READ_V2;
3031 read.buf_addr_lsw = param->paddr;
3032 read.buf_addr_msw = 0;
3033 read.buf_size = param->len;
3034 read.seq_id = param->uid;
Harmandeep Singheaf59b42012-06-05 21:46:02 -07003035 liomode = (NT_MODE | ASYNC_IO_MODE);
3036 if (ac->io_mode == liomode) {
3037 pr_info("%s: subtracting 32 for header\n", __func__);
3038 lbuf_addr_lsw = (read.buf_addr_lsw - 32);
3039 } else{
3040 lbuf_addr_lsw = read.buf_addr_lsw;
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07003041 }
3042
Harmandeep Singheaf59b42012-06-05 21:46:02 -07003043 list_for_each_safe(ptr, next, &ac->port[IN].mem_map_handle) {
3044 buf_node = list_entry(ptr, struct asm_buffer_node, list);
3045 if (buf_node->buf_addr_lsw == lbuf_addr_lsw) {
3046 read.mem_map_handle = buf_node->mmap_hdl;
3047 break;
3048 }
3049 }
Bharath Ramachandramurthy2e3168f2012-05-03 16:29:09 -07003050
3051 rc = apr_send_pkt(ac->apr, (uint32_t *) &read);
3052 if (rc < 0) {
3053 pr_debug("[%s] read op[0x%x]rc[%d]\n", __func__,
3054 read.hdr.opcode, rc);
3055 goto fail_cmd;
3056 }
3057 return 0;
3058fail_cmd:
3059 return -EINVAL;
3060}
3061
3062int q6asm_write(struct audio_client *ac, uint32_t len, uint32_t msw_ts,
3063 uint32_t lsw_ts, uint32_t flags)
3064{
3065 int rc = 0;
3066 struct asm_data_cmd_write_v2 write;
3067 struct asm_buffer_node *buf_node = NULL;
3068 struct audio_port_data *port;
3069 struct audio_buffer *ab;
3070 int dsp_buf = 0;
3071
3072 if (!ac || ac->apr == NULL) {
3073 pr_err("APR handle NULL\n");
3074 return -EINVAL;
3075 }
3076 pr_debug("%s: session[%d] len=%d", __func__, ac->session, len);
3077 if (ac->io_mode == SYNC_IO_MODE) {
3078 port = &ac->port[IN];
3079
3080 q6asm_add_hdr(ac, &write.hdr, sizeof(write),
3081 FALSE);
3082 mutex_lock(&port->lock);
3083
3084 dsp_buf = port->dsp_buf;
3085 ab = &port->buf[dsp_buf];
3086
3087 write.hdr.token = port->dsp_buf;
3088 write.hdr.opcode = ASM_DATA_CMD_WRITE_V2;
3089 write.buf_addr_lsw = ab->phys;
3090 write.buf_addr_msw = 0;
3091 write.buf_size = len;
3092 write.seq_id = port->dsp_buf;
3093 write.timestamp_lsw = lsw_ts;
3094 write.timestamp_msw = msw_ts;
3095 /* Use 0xFF00 for disabling timestamps */
3096 if (flags == 0xFF00)
3097 write.flags = (0x00000000 | (flags & 0x800000FF));
3098 else
3099 write.flags = (0x80000000 | flags);
3100 port->dsp_buf = (port->dsp_buf + 1) & (port->max_buf_cnt - 1);
3101 buf_node = list_first_entry(&ac->port[IN].mem_map_handle,
3102 struct asm_buffer_node,
3103 list);
3104 write.mem_map_handle = buf_node->mmap_hdl;
3105
Harmandeep Singheaf59b42012-06-05 21:46:02 -07003106 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 -07003107 , __func__,
3108 ab->phys,
3109 write.buf_addr_lsw,
3110 write.hdr.token,
3111 write.seq_id,
3112 write.buf_size,
3113 write.mem_map_handle);
3114 mutex_unlock(&port->lock);
3115
3116 config_debug_fs_write(ab);
3117
3118 rc = apr_send_pkt(ac->apr, (uint32_t *) &write);
3119 if (rc < 0) {
3120 pr_err("write op[0x%x]rc[%d]\n", write.hdr.opcode, rc);
3121 goto fail_cmd;
3122 }
3123 pr_debug("%s: WRITE SUCCESS\n", __func__);
3124 return 0;
3125 }
3126fail_cmd:
3127 return -EINVAL;
3128}
3129
3130int q6asm_write_nolock(struct audio_client *ac, uint32_t len, uint32_t msw_ts,
3131 uint32_t lsw_ts, uint32_t flags)
3132{
3133 int rc = 0;
3134 struct asm_data_cmd_write_v2 write;
3135 struct asm_buffer_node *buf_node = NULL;
3136 struct audio_port_data *port;
3137 struct audio_buffer *ab;
3138 int dsp_buf = 0;
3139
3140 if (!ac || ac->apr == NULL) {
3141 pr_err("APR handle NULL\n");
3142 return -EINVAL;
3143 }
3144 pr_debug("%s: session[%d] len=%d", __func__, ac->session, len);
3145 if (ac->io_mode == SYNC_IO_MODE) {
3146 port = &ac->port[IN];
3147
3148 q6asm_add_hdr_async(ac, &write.hdr, sizeof(write),
3149 FALSE);
3150
3151 dsp_buf = port->dsp_buf;
3152 ab = &port->buf[dsp_buf];
3153
3154 write.hdr.token = port->dsp_buf;
3155 write.hdr.opcode = ASM_DATA_CMD_WRITE_V2;
3156 write.buf_addr_lsw = ab->phys;
3157 write.buf_addr_msw = 0;
3158 write.buf_size = len;
3159 write.seq_id = port->dsp_buf;
3160 write.timestamp_lsw = lsw_ts;
3161 write.timestamp_msw = msw_ts;
3162 buf_node = list_first_entry(&ac->port[IN].mem_map_handle,
3163 struct asm_buffer_node,
3164 list);
3165 write.mem_map_handle = buf_node->mmap_hdl;
3166 /* Use 0xFF00 for disabling timestamps */
3167 if (flags == 0xFF00)
3168 write.flags = (0x00000000 | (flags & 0x800000FF));
3169 else
3170 write.flags = (0x80000000 | flags);
3171 port->dsp_buf = (port->dsp_buf + 1) & (port->max_buf_cnt - 1);
3172
Harmandeep Singheaf59b42012-06-05 21:46:02 -07003173 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 -07003174 , __func__,
3175 ab->phys,
3176 write.buf_addr_lsw,
3177 write.hdr.token,
3178 write.seq_id,
3179 write.buf_size,
3180 write.mem_map_handle);
3181
3182 rc = apr_send_pkt(ac->apr, (uint32_t *) &write);
3183 if (rc < 0) {
3184 pr_err("write op[0x%x]rc[%d]\n", write.hdr.opcode, rc);
3185 goto fail_cmd;
3186 }
3187 pr_debug("%s: WRITE SUCCESS\n", __func__);
3188 return 0;
3189 }
3190fail_cmd:
3191 return -EINVAL;
3192}
3193
3194uint64_t q6asm_get_session_time(struct audio_client *ac)
3195{
3196 struct apr_hdr hdr;
3197 int rc;
3198
3199 if (!ac || ac->apr == NULL) {
3200 pr_err("APR handle NULL\n");
3201 return -EINVAL;
3202 }
3203 q6asm_add_hdr(ac, &hdr, sizeof(hdr), TRUE);
3204 hdr.opcode = ASM_SESSION_CMD_GET_SESSIONTIME_V3;
3205 atomic_set(&ac->cmd_state, 1);
3206
3207 pr_debug("%s: session[%d]opcode[0x%x]\n", __func__,
3208 ac->session,
3209 hdr.opcode);
3210 rc = apr_send_pkt(ac->apr, (uint32_t *) &hdr);
3211 if (rc < 0) {
3212 pr_err("Commmand 0x%x failed\n", hdr.opcode);
3213 goto fail_cmd;
3214 }
3215 rc = wait_event_timeout(ac->cmd_wait,
3216 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
3217 if (!rc) {
3218 pr_err("%s: timeout in getting session time from DSP\n",
3219 __func__);
3220 goto fail_cmd;
3221 }
3222 return ac->time_stamp;
3223
3224fail_cmd:
3225 return -EINVAL;
3226}
3227
3228int q6asm_cmd(struct audio_client *ac, int cmd)
3229{
3230 struct apr_hdr hdr;
3231 int rc;
3232 atomic_t *state;
3233 int cnt = 0;
3234
3235 if (!ac || ac->apr == NULL) {
3236 pr_err("APR handle NULL\n");
3237 return -EINVAL;
3238 }
3239 q6asm_add_hdr(ac, &hdr, sizeof(hdr), TRUE);
3240 switch (cmd) {
3241 case CMD_PAUSE:
3242 pr_debug("%s:CMD_PAUSE\n", __func__);
3243 hdr.opcode = ASM_SESSION_CMD_PAUSE;
3244 state = &ac->cmd_state;
3245 break;
3246 case CMD_FLUSH:
3247 pr_debug("%s:CMD_FLUSH\n", __func__);
3248 hdr.opcode = ASM_STREAM_CMD_FLUSH;
3249 state = &ac->cmd_state;
3250 break;
3251 case CMD_OUT_FLUSH:
3252 pr_debug("%s:CMD_OUT_FLUSH\n", __func__);
3253 hdr.opcode = ASM_STREAM_CMD_FLUSH_READBUFS;
3254 state = &ac->cmd_state;
3255 break;
3256 case CMD_EOS:
3257 pr_debug("%s:CMD_EOS\n", __func__);
3258 hdr.opcode = ASM_DATA_CMD_EOS;
3259 atomic_set(&ac->cmd_state, 0);
3260 state = &ac->cmd_state;
3261 break;
3262 case CMD_CLOSE:
3263 pr_debug("%s:CMD_CLOSE\n", __func__);
3264 hdr.opcode = ASM_STREAM_CMD_CLOSE;
3265 state = &ac->cmd_state;
3266 break;
3267 default:
3268 pr_err("Invalid format[%d]\n", cmd);
3269 goto fail_cmd;
3270 }
3271 pr_debug("%s:session[%d]opcode[0x%x] ", __func__,
3272 ac->session,
3273 hdr.opcode);
3274 rc = apr_send_pkt(ac->apr, (uint32_t *) &hdr);
3275 if (rc < 0) {
3276 pr_err("Commmand 0x%x failed\n", hdr.opcode);
3277 goto fail_cmd;
3278 }
3279 rc = wait_event_timeout(ac->cmd_wait, (atomic_read(state) == 0), 5*HZ);
3280 if (!rc) {
3281 pr_err("timeout. waited for response opcode[0x%x]\n",
3282 hdr.opcode);
3283 goto fail_cmd;
3284 }
3285 if (cmd == CMD_FLUSH)
3286 q6asm_reset_buf_state(ac);
3287 if (cmd == CMD_CLOSE) {
3288 /* check if DSP return all buffers */
3289 if (ac->port[IN].buf) {
3290 for (cnt = 0; cnt < ac->port[IN].max_buf_cnt;
3291 cnt++) {
3292 if (ac->port[IN].buf[cnt].used == IN) {
3293 pr_debug("Write Buf[%d] not returned\n",
3294 cnt);
3295 }
3296 }
3297 }
3298 if (ac->port[OUT].buf) {
3299 for (cnt = 0; cnt < ac->port[OUT].max_buf_cnt; cnt++) {
3300 if (ac->port[OUT].buf[cnt].used == OUT) {
3301 pr_debug("Read Buf[%d] not returned\n",
3302 cnt);
3303 }
3304 }
3305 }
3306 }
3307 return 0;
3308fail_cmd:
3309 return -EINVAL;
3310}
3311
3312int q6asm_cmd_nowait(struct audio_client *ac, int cmd)
3313{
3314 struct apr_hdr hdr;
3315 int rc;
3316
3317 if (!ac || ac->apr == NULL) {
3318 pr_err("%s:APR handle NULL\n", __func__);
3319 return -EINVAL;
3320 }
3321 q6asm_add_hdr_async(ac, &hdr, sizeof(hdr), TRUE);
3322 switch (cmd) {
3323 case CMD_PAUSE:
3324 pr_debug("%s:CMD_PAUSE\n", __func__);
3325 hdr.opcode = ASM_SESSION_CMD_PAUSE;
3326 break;
3327 case CMD_EOS:
3328 pr_debug("%s:CMD_EOS\n", __func__);
3329 hdr.opcode = ASM_DATA_CMD_EOS;
3330 break;
3331 default:
3332 pr_err("%s:Invalid format[%d]\n", __func__, cmd);
3333 goto fail_cmd;
3334 }
3335 pr_debug("%s:session[%d]opcode[0x%x] ", __func__,
3336 ac->session,
3337 hdr.opcode);
3338 rc = apr_send_pkt(ac->apr, (uint32_t *) &hdr);
3339 if (rc < 0) {
3340 pr_err("%s:Commmand 0x%x failed\n", __func__, hdr.opcode);
3341 goto fail_cmd;
3342 }
3343 return 0;
3344fail_cmd:
3345 return -EINVAL;
3346}
3347
3348static void q6asm_reset_buf_state(struct audio_client *ac)
3349{
3350 int cnt = 0;
3351 int loopcnt = 0;
3352 struct audio_port_data *port = NULL;
3353
3354 if (ac->io_mode == SYNC_IO_MODE) {
3355 mutex_lock(&ac->cmd_lock);
3356 for (loopcnt = 0; loopcnt <= OUT; loopcnt++) {
3357 port = &ac->port[loopcnt];
3358 cnt = port->max_buf_cnt - 1;
3359 port->dsp_buf = 0;
3360 port->cpu_buf = 0;
3361 while (cnt >= 0) {
3362 if (!port->buf)
3363 continue;
3364 port->buf[cnt].used = 1;
3365 cnt--;
3366 }
3367 }
3368 mutex_unlock(&ac->cmd_lock);
3369 }
3370}
3371
3372int q6asm_reg_tx_overflow(struct audio_client *ac, uint16_t enable)
3373{
3374 struct asm_session_cmd_regx_overflow tx_overflow;
3375 int rc;
3376
3377 if (!ac || ac->apr == NULL) {
3378 pr_err("APR handle NULL\n");
3379 return -EINVAL;
3380 }
3381 pr_debug("%s:session[%d]enable[%d]\n", __func__,
3382 ac->session, enable);
3383 q6asm_add_hdr(ac, &tx_overflow.hdr, sizeof(tx_overflow), TRUE);
3384
3385 tx_overflow.hdr.opcode = \
3386 ASM_SESSION_CMD_REGISTER_FORX_OVERFLOW_EVENTS;
3387 /* tx overflow event: enable */
3388 tx_overflow.enable_flag = enable;
3389
3390 rc = apr_send_pkt(ac->apr, (uint32_t *) &tx_overflow);
3391 if (rc < 0) {
3392 pr_err("tx overflow op[0x%x]rc[%d]\n", \
3393 tx_overflow.hdr.opcode, rc);
3394 goto fail_cmd;
3395 }
3396 rc = wait_event_timeout(ac->cmd_wait,
3397 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
3398 if (!rc) {
3399 pr_err("timeout. waited for tx overflow\n");
3400 goto fail_cmd;
3401 }
3402 return 0;
3403fail_cmd:
3404 return -EINVAL;
3405}
3406
3407int q6asm_get_apr_service_id(int session_id)
3408{
3409 pr_debug("%s\n", __func__);
3410
3411 if (session_id < 0 || session_id > SESSION_MAX) {
3412 pr_err("%s: invalid session_id = %d\n", __func__, session_id);
3413 return -EINVAL;
3414 }
3415
3416 return ((struct apr_svc *)session[session_id]->apr)->id;
3417}
3418
3419
3420static int __init q6asm_init(void)
3421{
3422 pr_debug("%s\n", __func__);
3423 memset(session, 0, sizeof(session));
3424
3425 config_debug_fs_init();
3426
3427 return 0;
3428}
3429
3430device_initcall(q6asm_init);