blob: 50011a14be90ae574bfb747891c3362a73000d34 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001
2/*
Kiran Kandi5e809b02012-01-31 00:24:33 -08003 * Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004 * Author: Brian Swetland <swetland@google.com>
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16#include <linux/fs.h>
17#include <linux/mutex.h>
18#include <linux/wait.h>
19#include <linux/miscdevice.h>
20#include <linux/uaccess.h>
21#include <linux/sched.h>
22#include <linux/dma-mapping.h>
23#include <linux/miscdevice.h>
24#include <linux/delay.h>
25#include <linux/spinlock.h>
26#include <linux/slab.h>
27#include <linux/msm_audio.h>
28#include <linux/android_pmem.h>
29#include <linux/memory_alloc.h>
Ben Rombergerfce8f512011-07-18 16:46:09 -070030#include <linux/debugfs.h>
31#include <linux/time.h>
32#include <linux/atomic.h>
33
34#include <asm/ioctls.h>
35
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036#include <mach/memory.h>
37#include <mach/debug_mm.h>
38#include <mach/peripheral-loader.h>
39#include <mach/qdsp6v2/audio_acdb.h>
40#include <mach/qdsp6v2/rtac.h>
Ben Rombergerfce8f512011-07-18 16:46:09 -070041
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070042#include <sound/apr_audio.h>
43#include <sound/q6asm.h>
Ben Rombergerfce8f512011-07-18 16:46:09 -070044
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045
46#define TRUE 0x01
47#define FALSE 0x00
48#define READDONE_IDX_STATUS 0
49#define READDONE_IDX_BUFFER 1
50#define READDONE_IDX_SIZE 2
51#define READDONE_IDX_OFFSET 3
52#define READDONE_IDX_MSW_TS 4
53#define READDONE_IDX_LSW_TS 5
54#define READDONE_IDX_FLAGS 6
55#define READDONE_IDX_NUMFRAMES 7
56#define READDONE_IDX_ID 8
Rajesha Kini3498c932011-07-19 19:58:27 +053057#ifdef CONFIG_DEBUG_FS
58#define OUT_BUFFER_SIZE 56
59#define IN_BUFFER_SIZE 24
60#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070061static DEFINE_MUTEX(session_lock);
62
63/* session id: 0 reserved */
64static struct audio_client *session[SESSION_MAX+1];
65static int32_t q6asm_mmapcallback(struct apr_client_data *data, void *priv);
66static int32_t q6asm_callback(struct apr_client_data *data, void *priv);
67static void q6asm_add_hdr(struct audio_client *ac, struct apr_hdr *hdr,
68 uint32_t pkt_size, uint32_t cmd_flg);
69static void q6asm_add_hdr_async(struct audio_client *ac, struct apr_hdr *hdr,
70 uint32_t pkt_size, uint32_t cmd_flg);
71static int q6asm_memory_map_regions(struct audio_client *ac, int dir,
72 uint32_t bufsz, uint32_t bufcnt);
73static int q6asm_memory_unmap_regions(struct audio_client *ac, int dir,
74 uint32_t bufsz, uint32_t bufcnt);
75
76static void q6asm_reset_buf_state(struct audio_client *ac);
77
Rajesha Kini3498c932011-07-19 19:58:27 +053078#ifdef CONFIG_DEBUG_FS
79static struct timeval out_cold_tv;
80static struct timeval out_warm_tv;
81static struct timeval out_cont_tv;
82static struct timeval in_cont_tv;
83static long out_enable_flag;
84static long in_enable_flag;
85static struct dentry *out_dentry;
86static struct dentry *in_dentry;
87static int in_cont_index;
88/*This var is used to keep track of first write done for cold output latency */
89static int out_cold_index;
90static char *out_buffer;
91static char *in_buffer;
92static int audio_output_latency_dbgfs_open(struct inode *inode,
93 struct file *file)
94{
95 file->private_data = inode->i_private;
96 return 0;
97}
98static ssize_t audio_output_latency_dbgfs_read(struct file *file,
99 char __user *buf, size_t count, loff_t *ppos)
100{
101 snprintf(out_buffer, OUT_BUFFER_SIZE, "%ld,%ld,%ld,%ld,%ld,%ld,",\
102 out_cold_tv.tv_sec, out_cold_tv.tv_usec, out_warm_tv.tv_sec,\
103 out_warm_tv.tv_usec, out_cont_tv.tv_sec, out_cont_tv.tv_usec);
104 return simple_read_from_buffer(buf, OUT_BUFFER_SIZE, ppos,
105 out_buffer, OUT_BUFFER_SIZE);
106}
107static ssize_t audio_output_latency_dbgfs_write(struct file *file,
108 const char __user *buf, size_t count, loff_t *ppos)
109{
110 char *temp;
111
112 if (count > 2*sizeof(char))
113 return -EINVAL;
114 else
115 temp = kmalloc(2*sizeof(char), GFP_KERNEL);
116
117 out_cold_index = 0;
118
119 if (temp) {
120 if (copy_from_user(temp, buf, 2*sizeof(char))) {
121 kfree(temp);
122 return -EFAULT;
123 }
124 if (!strict_strtol(temp, 10, &out_enable_flag)) {
125 kfree(temp);
126 return count;
127 }
128 kfree(temp);
129 }
130 return -EINVAL;
131}
132static const struct file_operations audio_output_latency_debug_fops = {
133 .open = audio_output_latency_dbgfs_open,
134 .read = audio_output_latency_dbgfs_read,
135 .write = audio_output_latency_dbgfs_write
136};
137
138static int audio_input_latency_dbgfs_open(struct inode *inode,
139 struct file *file)
140{
141 file->private_data = inode->i_private;
142 return 0;
143}
144static ssize_t audio_input_latency_dbgfs_read(struct file *file,
145 char __user *buf, size_t count, loff_t *ppos)
146{
147 snprintf(in_buffer, IN_BUFFER_SIZE, "%ld,%ld,",\
148 in_cont_tv.tv_sec, in_cont_tv.tv_usec);
149 return simple_read_from_buffer(buf, IN_BUFFER_SIZE, ppos,
150 in_buffer, IN_BUFFER_SIZE);
151}
152static ssize_t audio_input_latency_dbgfs_write(struct file *file,
153 const char __user *buf, size_t count, loff_t *ppos)
154{
155 char *temp;
156
157 if (count > 2*sizeof(char))
158 return -EINVAL;
159 else
160 temp = kmalloc(2*sizeof(char), GFP_KERNEL);
161 if (temp) {
162 if (copy_from_user(temp, buf, 2*sizeof(char))) {
163 kfree(temp);
164 return -EFAULT;
165 }
166 if (!strict_strtol(temp, 10, &in_enable_flag)) {
167 kfree(temp);
168 return count;
169 }
170 kfree(temp);
171 }
172 return -EINVAL;
173}
174static const struct file_operations audio_input_latency_debug_fops = {
175 .open = audio_input_latency_dbgfs_open,
176 .read = audio_input_latency_dbgfs_read,
177 .write = audio_input_latency_dbgfs_write
178};
179#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180struct asm_mmap {
181 atomic_t ref_cnt;
182 atomic_t cmd_state;
183 wait_queue_head_t cmd_wait;
184 void *apr;
185};
186
187static struct asm_mmap this_mmap;
188
189static int q6asm_session_alloc(struct audio_client *ac)
190{
191 int n;
192 mutex_lock(&session_lock);
193 for (n = 1; n <= SESSION_MAX; n++) {
194 if (!session[n]) {
195 session[n] = ac;
196 mutex_unlock(&session_lock);
197 return n;
198 }
199 }
200 mutex_unlock(&session_lock);
201 return -ENOMEM;
202}
203
204static void q6asm_session_free(struct audio_client *ac)
205{
206 pr_debug("%s: sessionid[%d]\n", __func__, ac->session);
Ben Romberger93d4d2d2011-10-19 23:04:02 -0700207 rtac_remove_popp_from_adm_devices(ac->session);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208 mutex_lock(&session_lock);
209 session[ac->session] = 0;
210 mutex_unlock(&session_lock);
211 ac->session = 0;
212 return;
213}
214
215int q6asm_audio_client_buf_free(unsigned int dir,
216 struct audio_client *ac)
217{
218 struct audio_port_data *port;
219 int cnt = 0;
220 int rc = 0;
221 pr_debug("%s: Session id %d\n", __func__, ac->session);
222 mutex_lock(&ac->cmd_lock);
223 if (ac->io_mode == SYNC_IO_MODE) {
224 port = &ac->port[dir];
225 if (!port->buf) {
226 mutex_unlock(&ac->cmd_lock);
227 return 0;
228 }
229 cnt = port->max_buf_cnt - 1;
230
231 if (cnt >= 0) {
232 rc = q6asm_memory_unmap_regions(ac, dir,
233 port->buf[0].size,
234 port->max_buf_cnt);
235 if (rc < 0)
236 pr_err("%s CMD Memory_unmap_regions failed\n",
237 __func__);
238 }
239
240 while (cnt >= 0) {
241 if (port->buf[cnt].data) {
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800242#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
243 ion_unmap_kernel(port->buf[cnt].client,
244 port->buf[cnt].handle);
245 ion_free(port->buf[cnt].client,
246 port->buf[cnt].handle);
247 ion_client_destroy(port->buf[cnt].client);
248#else
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700249 pr_debug("%s:data[%p]phys[%p][%p] cnt[%d] mem_buffer[%p]\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250 __func__, (void *)port->buf[cnt].data,
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700251 (void *)port->buf[cnt].phys,
252 (void *)&port->buf[cnt].phys, cnt,
253 (void *)port->buf[cnt].mem_buffer);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254 if (IS_ERR((void *)port->buf[cnt].mem_buffer))
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700255 pr_err("%s:mem buffer invalid, error = %ld\n",
256 __func__,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257 PTR_ERR((void *)port->buf[cnt].mem_buffer));
258 else {
Laura Abbottea3e7b62012-04-30 15:59:21 -0700259 if (iounmap(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260 port->buf[cnt].mem_buffer) < 0)
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700261 pr_err("%s: unmap buffer failed\n",
262 __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263 }
264 free_contiguous_memory_by_paddr(
265 port->buf[cnt].phys);
266
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800267#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700268 port->buf[cnt].data = NULL;
269 port->buf[cnt].phys = 0;
270 --(port->max_buf_cnt);
271 }
272 --cnt;
273 }
274 kfree(port->buf);
275 port->buf = NULL;
276 }
277 mutex_unlock(&ac->cmd_lock);
278 return 0;
279}
280
281int q6asm_audio_client_buf_free_contiguous(unsigned int dir,
282 struct audio_client *ac)
283{
284 struct audio_port_data *port;
285 int cnt = 0;
286 int rc = 0;
287 pr_debug("%s: Session id %d\n", __func__, ac->session);
288 mutex_lock(&ac->cmd_lock);
289 port = &ac->port[dir];
290 if (!port->buf) {
291 mutex_unlock(&ac->cmd_lock);
292 return 0;
293 }
294 cnt = port->max_buf_cnt - 1;
295
296 if (cnt >= 0) {
Deepa Madiregama7d52a402011-07-13 20:28:36 +0530297 rc = q6asm_memory_unmap(ac, port->buf[0].phys, dir);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298 if (rc < 0)
299 pr_err("%s CMD Memory_unmap_regions failed\n",
300 __func__);
301 }
302
303 if (port->buf[0].data) {
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800304#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
305 ion_unmap_kernel(port->buf[0].client, port->buf[0].handle);
306 ion_free(port->buf[0].client, port->buf[0].handle);
307 ion_client_destroy(port->buf[0].client);
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700308 pr_debug("%s:data[%p]phys[%p][%p], client[%p] handle[%p]\n",
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800309 __func__,
310 (void *)port->buf[0].data,
311 (void *)port->buf[0].phys,
312 (void *)&port->buf[0].phys,
313 (void *)port->buf[0].client,
314 (void *)port->buf[0].handle);
315#else
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700316 pr_debug("%s:data[%p]phys[%p][%p] mem_buffer[%p]\n",
Harmandeep Singh2f7c23c2011-11-04 11:57:35 -0700317 __func__,
318 (void *)port->buf[0].data,
319 (void *)port->buf[0].phys,
320 (void *)&port->buf[0].phys,
321 (void *)port->buf[0].mem_buffer);
322 if (IS_ERR((void *)port->buf[0].mem_buffer))
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700323 pr_err("%s:mem buffer invalid, error = %ld\n",
324 __func__,
Harmandeep Singh2f7c23c2011-11-04 11:57:35 -0700325 PTR_ERR((void *)port->buf[0].mem_buffer));
326 else {
Laura Abbottea3e7b62012-04-30 15:59:21 -0700327 if (iounmap(
Harmandeep Singh2f7c23c2011-11-04 11:57:35 -0700328 port->buf[0].mem_buffer) < 0)
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700329 pr_err("%s: unmap buffer failed\n", __func__);
Harmandeep Singh2f7c23c2011-11-04 11:57:35 -0700330 }
331 free_contiguous_memory_by_paddr(port->buf[0].phys);
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800332#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333 }
Harmandeep Singh2f7c23c2011-11-04 11:57:35 -0700334
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700335 while (cnt >= 0) {
336 port->buf[cnt].data = NULL;
337 port->buf[cnt].phys = 0;
338 cnt--;
339 }
340 port->max_buf_cnt = 0;
341 kfree(port->buf);
342 port->buf = NULL;
343 mutex_unlock(&ac->cmd_lock);
344 return 0;
345}
346
347void q6asm_audio_client_free(struct audio_client *ac)
348{
349 int loopcnt;
350 struct audio_port_data *port;
351 if (!ac || !ac->session)
352 return;
353 pr_debug("%s: Session id %d\n", __func__, ac->session);
354 if (ac->io_mode == SYNC_IO_MODE) {
355 for (loopcnt = 0; loopcnt <= OUT; loopcnt++) {
356 port = &ac->port[loopcnt];
357 if (!port->buf)
358 continue;
359 pr_debug("%s:loopcnt = %d\n", __func__, loopcnt);
360 q6asm_audio_client_buf_free(loopcnt, ac);
361 }
362 }
363
364 apr_deregister(ac->apr);
365 q6asm_session_free(ac);
366
367 pr_debug("%s: APR De-Register\n", __func__);
368 if (atomic_read(&this_mmap.ref_cnt) <= 0) {
369 pr_err("%s: APR Common Port Already Closed\n", __func__);
370 goto done;
371 }
372
373 atomic_dec(&this_mmap.ref_cnt);
374 if (atomic_read(&this_mmap.ref_cnt) == 0) {
375 apr_deregister(this_mmap.apr);
376 pr_debug("%s:APR De-Register common port\n", __func__);
377 }
378done:
379 kfree(ac);
380 return;
381}
382
383int q6asm_set_io_mode(struct audio_client *ac, uint32_t mode)
384{
385 if (ac == NULL) {
386 pr_err("%s APR handle NULL\n", __func__);
387 return -EINVAL;
388 }
389 if ((mode == ASYNC_IO_MODE) || (mode == SYNC_IO_MODE)) {
390 ac->io_mode = mode;
391 pr_debug("%s:Set Mode to %d\n", __func__, ac->io_mode);
392 return 0;
393 } else {
394 pr_err("%s:Not an valid IO Mode:%d\n", __func__, ac->io_mode);
395 return -EINVAL;
396 }
397}
398
399struct audio_client *q6asm_audio_client_alloc(app_cb cb, void *priv)
400{
401 struct audio_client *ac;
402 int n;
403 int lcnt = 0;
404
405 ac = kzalloc(sizeof(struct audio_client), GFP_KERNEL);
406 if (!ac)
407 return NULL;
408 n = q6asm_session_alloc(ac);
409 if (n <= 0)
410 goto fail_session;
411 ac->session = n;
412 ac->cb = cb;
413 ac->priv = priv;
414 ac->io_mode = SYNC_IO_MODE;
415 ac->apr = apr_register("ADSP", "ASM", \
416 (apr_fn)q6asm_callback,\
417 ((ac->session) << 8 | 0x0001),\
418 ac);
419
420 if (ac->apr == NULL) {
421 pr_err("%s Registration with APR failed\n", __func__);
422 goto fail;
423 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700424 rtac_set_asm_handle(n, ac->apr);
Ben Rombergerfce8f512011-07-18 16:46:09 -0700425
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426 pr_debug("%s Registering the common port with APR\n", __func__);
427 if (atomic_read(&this_mmap.ref_cnt) == 0) {
428 this_mmap.apr = apr_register("ADSP", "ASM", \
429 (apr_fn)q6asm_mmapcallback,\
430 0x0FFFFFFFF, &this_mmap);
431 if (this_mmap.apr == NULL) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700432 pr_debug("%s Unable to register APR ASM common port\n",
433 __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700434 goto fail;
435 }
436 }
437
438 atomic_inc(&this_mmap.ref_cnt);
439 init_waitqueue_head(&ac->cmd_wait);
440 init_waitqueue_head(&ac->time_wait);
441 atomic_set(&ac->time_flag, 1);
442 mutex_init(&ac->cmd_lock);
443 for (lcnt = 0; lcnt <= OUT; lcnt++) {
444 mutex_init(&ac->port[lcnt].lock);
445 spin_lock_init(&ac->port[lcnt].dsp_lock);
446 }
447 atomic_set(&ac->cmd_state, 0);
448
449 pr_debug("%s: session[%d]\n", __func__, ac->session);
450
451 return ac;
452fail:
453 q6asm_audio_client_free(ac);
454 return NULL;
455fail_session:
456 kfree(ac);
457 return NULL;
458}
459
Ben Romberger61754dc2011-10-31 18:25:41 -0700460struct audio_client *q6asm_get_audio_client(int session_id)
461{
462 if ((session_id <= 0) || (session_id > SESSION_MAX)) {
463 pr_err("%s: invalid session: %d\n", __func__, session_id);
464 goto err;
465 }
466
467 if (!session[session_id]) {
468 pr_err("%s: session not active: %d\n", __func__, session_id);
469 goto err;
470 }
471
472 return session[session_id];
473err:
474 return NULL;
475}
476
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477int q6asm_audio_client_buf_alloc(unsigned int dir,
478 struct audio_client *ac,
479 unsigned int bufsz,
480 unsigned int bufcnt)
481{
482 int cnt = 0;
483 int rc = 0;
484 struct audio_buffer *buf;
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800485#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
486 int len;
487#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700488
489 if (!(ac) || ((dir != IN) && (dir != OUT)))
490 return -EINVAL;
491
492 pr_debug("%s: session[%d]bufsz[%d]bufcnt[%d]\n", __func__, ac->session,
493 bufsz, bufcnt);
494
495 if (ac->session <= 0 || ac->session > 8)
496 goto fail;
497
498 if (ac->io_mode == SYNC_IO_MODE) {
499 if (ac->port[dir].buf) {
500 pr_debug("%s: buffer already allocated\n", __func__);
501 return 0;
502 }
503 mutex_lock(&ac->cmd_lock);
504 buf = kzalloc(((sizeof(struct audio_buffer))*bufcnt),
505 GFP_KERNEL);
506
507 if (!buf) {
508 mutex_unlock(&ac->cmd_lock);
509 goto fail;
510 }
511
512 ac->port[dir].buf = buf;
513
514 while (cnt < bufcnt) {
515 if (bufsz > 0) {
516 if (!buf[cnt].data) {
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800517#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
518 buf[cnt].client = msm_ion_client_create
519 (UINT_MAX, "audio_client");
520 if (IS_ERR_OR_NULL((void *)
521 buf[cnt].client)) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700522 pr_err("%s: ION create client for AUDIO failed\n",
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800523 __func__);
Swaminathan Sathappan349fed62012-05-22 15:04:43 -0700524 mutex_unlock(&ac->cmd_lock);
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800525 goto fail;
526 }
527 buf[cnt].handle = ion_alloc
528 (buf[cnt].client, bufsz, SZ_4K,
529 (0x1 << ION_AUDIO_HEAP_ID));
530 if (IS_ERR_OR_NULL((void *)
531 buf[cnt].handle)) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700532 pr_err("%s: ION memory allocation for AUDIO failed\n",
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800533 __func__);
Swaminathan Sathappan349fed62012-05-22 15:04:43 -0700534 mutex_unlock(&ac->cmd_lock);
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800535 goto fail;
536 }
537
538 rc = ion_phys(buf[cnt].client,
539 buf[cnt].handle,
540 (ion_phys_addr_t *)
541 &buf[cnt].phys,
542 (size_t *)&len);
543 if (rc) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700544 pr_err("%s: ION Get Physical for AUDIO failed, rc = %d\n",
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800545 __func__, rc);
Swaminathan Sathappan349fed62012-05-22 15:04:43 -0700546 mutex_unlock(&ac->cmd_lock);
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800547 goto fail;
548 }
549
550 buf[cnt].data = ion_map_kernel
551 (buf[cnt].client, buf[cnt].handle,
552 0);
553 if (IS_ERR_OR_NULL((void *)
554 buf[cnt].data)) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700555 pr_err("%s: ION memory mapping for AUDIO failed\n",
556 __func__);
Swaminathan Sathappan349fed62012-05-22 15:04:43 -0700557 mutex_unlock(&ac->cmd_lock);
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800558 goto fail;
559 }
560 memset((void *)buf[cnt].data, 0, bufsz);
561#else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562 unsigned int flags = 0;
563 buf[cnt].phys =
564 allocate_contiguous_ebi_nomap(bufsz,
565 SZ_4K);
566 if (!buf[cnt].phys) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700567 pr_err("%s:Buf alloc failed size=%d\n",
568 __func__,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569 bufsz);
570 mutex_unlock(&ac->cmd_lock);
571 goto fail;
572 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573 buf[cnt].mem_buffer =
Laura Abbottea3e7b62012-04-30 15:59:21 -0700574 ioremap(buf[cnt].phys, bufsz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700575 if (IS_ERR(
576 (void *)buf[cnt].mem_buffer)) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700577 pr_err("%s:map_buffer failed, error = %ld\n",
578 __func__,
579 PTR_ERR((void *)buf[cnt].mem_buffer));
Jayasena Sangaraboina466e6b92012-01-17 21:10:55 -0800580 mutex_unlock(&ac->cmd_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700581 goto fail;
582 }
583 buf[cnt].data =
Laura Abbottea3e7b62012-04-30 15:59:21 -0700584 buf[cnt].mem_buffer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585 if (!buf[cnt].data) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700586 pr_err("%s:invalid vaddr, iomap failed\n",
587 __func__);
Jayasena Sangaraboina466e6b92012-01-17 21:10:55 -0800588 mutex_unlock(&ac->cmd_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700589 goto fail;
590 }
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800591#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700592 buf[cnt].used = 1;
593 buf[cnt].size = bufsz;
594 buf[cnt].actual_size = bufsz;
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800595 pr_debug("%s data[%p]phys[%p][%p]\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700596 __func__,
597 (void *)buf[cnt].data,
598 (void *)buf[cnt].phys,
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800599 (void *)&buf[cnt].phys);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600 cnt++;
601 }
602 }
603 }
604 ac->port[dir].max_buf_cnt = cnt;
605
606 mutex_unlock(&ac->cmd_lock);
607 rc = q6asm_memory_map_regions(ac, dir, bufsz, cnt);
608 if (rc < 0) {
609 pr_err("%s:CMD Memory_map_regions failed\n", __func__);
610 goto fail;
611 }
612 }
613 return 0;
614fail:
615 q6asm_audio_client_buf_free(dir, ac);
616 return -EINVAL;
617}
618
619int q6asm_audio_client_buf_alloc_contiguous(unsigned int dir,
620 struct audio_client *ac,
621 unsigned int bufsz,
622 unsigned int bufcnt)
623{
624 int cnt = 0;
625 int rc = 0;
626 struct audio_buffer *buf;
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800627#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
628 int len;
629#else
630 int flags = 0;
631#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632 if (!(ac) || ((dir != IN) && (dir != OUT)))
633 return -EINVAL;
634
635 pr_debug("%s: session[%d]bufsz[%d]bufcnt[%d]\n",
636 __func__, ac->session,
637 bufsz, bufcnt);
638
639 if (ac->session <= 0 || ac->session > 8)
640 goto fail;
641
642 if (ac->port[dir].buf) {
643 pr_debug("%s: buffer already allocated\n", __func__);
644 return 0;
645 }
646 mutex_lock(&ac->cmd_lock);
647 buf = kzalloc(((sizeof(struct audio_buffer))*bufcnt),
648 GFP_KERNEL);
649
650 if (!buf) {
651 mutex_unlock(&ac->cmd_lock);
652 goto fail;
653 }
654
655 ac->port[dir].buf = buf;
656
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800657#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
658 buf[0].client = msm_ion_client_create(UINT_MAX, "audio_client");
659 if (IS_ERR_OR_NULL((void *)buf[0].client)) {
660 pr_err("%s: ION create client for AUDIO failed\n", __func__);
Swaminathan Sathappan349fed62012-05-22 15:04:43 -0700661 mutex_unlock(&ac->cmd_lock);
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800662 goto fail;
663 }
664 buf[0].handle = ion_alloc(buf[0].client, bufsz * bufcnt, SZ_4K,
665 (0x1 << ION_AUDIO_HEAP_ID));
666 if (IS_ERR_OR_NULL((void *) buf[0].handle)) {
667 pr_err("%s: ION memory allocation for AUDIO failed\n",
668 __func__);
Swaminathan Sathappan349fed62012-05-22 15:04:43 -0700669 mutex_unlock(&ac->cmd_lock);
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800670 goto fail;
671 }
672
673 rc = ion_phys(buf[0].client, buf[0].handle,
674 (ion_phys_addr_t *)&buf[0].phys, (size_t *)&len);
675 if (rc) {
676 pr_err("%s: ION Get Physical for AUDIO failed, rc = %d\n",
677 __func__, rc);
Swaminathan Sathappan349fed62012-05-22 15:04:43 -0700678 mutex_unlock(&ac->cmd_lock);
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800679 goto fail;
680 }
681
682 buf[0].data = ion_map_kernel(buf[0].client, buf[0].handle, 0);
683 if (IS_ERR_OR_NULL((void *) buf[0].data)) {
684 pr_err("%s: ION memory mapping for AUDIO failed\n", __func__);
Swaminathan Sathappan349fed62012-05-22 15:04:43 -0700685 mutex_unlock(&ac->cmd_lock);
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800686 goto fail;
687 }
688 memset((void *)buf[0].data, 0, (bufsz * bufcnt));
689#else
Harmandeep Singh2f7c23c2011-11-04 11:57:35 -0700690 buf[0].phys = allocate_contiguous_ebi_nomap(bufsz * bufcnt,
691 SZ_4K);
692 if (!buf[0].phys) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700693 pr_err("%s:Buf alloc failed size=%d, bufcnt=%d\n",
694 __func__, bufsz, bufcnt);
Harmandeep Singh2f7c23c2011-11-04 11:57:35 -0700695 mutex_unlock(&ac->cmd_lock);
696 goto fail;
697 }
698
Laura Abbottea3e7b62012-04-30 15:59:21 -0700699 buf[0].mem_buffer = ioremap(buf[0].phys, bufsz * bufcnt);
Harmandeep Singh2f7c23c2011-11-04 11:57:35 -0700700 if (IS_ERR((void *)buf[cnt].mem_buffer)) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700701 pr_err("%s:map_buffer failed, error = %ld\n",
Harmandeep Singh2f7c23c2011-11-04 11:57:35 -0700702 __func__, PTR_ERR((void *)buf[0].mem_buffer));
703
704 mutex_unlock(&ac->cmd_lock);
705 goto fail;
706 }
Laura Abbottea3e7b62012-04-30 15:59:21 -0700707 buf[0].data = buf[0].mem_buffer;
Swaminathan Sathappanbdc55082012-02-16 22:47:40 -0800708#endif
Harmandeep Singh2f7c23c2011-11-04 11:57:35 -0700709 if (!buf[0].data) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700710 pr_err("%s:invalid vaddr, iomap failed\n", __func__);
Harmandeep Singh2f7c23c2011-11-04 11:57:35 -0700711 mutex_unlock(&ac->cmd_lock);
712 goto fail;
713 }
714
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700715 buf[0].used = dir ^ 1;
716 buf[0].size = bufsz;
717 buf[0].actual_size = bufsz;
718 cnt = 1;
719 while (cnt < bufcnt) {
720 if (bufsz > 0) {
721 buf[cnt].data = buf[0].data + (cnt * bufsz);
722 buf[cnt].phys = buf[0].phys + (cnt * bufsz);
723 if (!buf[cnt].data) {
724 pr_err("%s Buf alloc failed\n",
725 __func__);
726 mutex_unlock(&ac->cmd_lock);
727 goto fail;
728 }
729 buf[cnt].used = dir ^ 1;
730 buf[cnt].size = bufsz;
731 buf[cnt].actual_size = bufsz;
732 pr_debug("%s data[%p]phys[%p][%p]\n", __func__,
733 (void *)buf[cnt].data,
734 (void *)buf[cnt].phys,
735 (void *)&buf[cnt].phys);
736 }
737 cnt++;
738 }
739 ac->port[dir].max_buf_cnt = cnt;
740 mutex_unlock(&ac->cmd_lock);
741 rc = q6asm_memory_map(ac, buf[0].phys, dir, bufsz, cnt);
742 if (rc < 0) {
743 pr_err("%s:CMD Memory_map_regions failed\n", __func__);
744 goto fail;
745 }
746 return 0;
747fail:
748 q6asm_audio_client_buf_free_contiguous(dir, ac);
749 return -EINVAL;
750}
751
752static int32_t q6asm_mmapcallback(struct apr_client_data *data, void *priv)
753{
754 uint32_t token;
755 uint32_t *payload = data->payload;
756
757 if (data->opcode == RESET_EVENTS) {
758 pr_debug("%s: Reset event is received: %d %d apr[%p]\n",
759 __func__,
760 data->reset_event,
761 data->reset_proc,
762 this_mmap.apr);
763 apr_reset(this_mmap.apr);
764 this_mmap.apr = NULL;
765 atomic_set(&this_mmap.cmd_state, 0);
766 return 0;
767 }
768
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700769 pr_debug("%s:ptr0[0x%x]ptr1[0x%x]opcode[0x%x] token[0x%x]payload_s[%d] src[%d] dest[%d]\n",
770 __func__, payload[0], payload[1], data->opcode, data->token,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700771 data->payload_size, data->src_port, data->dest_port);
772
773 if (data->opcode == APR_BASIC_RSP_RESULT) {
774 token = data->token;
775 switch (payload[0]) {
776 case ASM_SESSION_CMD_MEMORY_MAP:
777 case ASM_SESSION_CMD_MEMORY_UNMAP:
778 case ASM_SESSION_CMD_MEMORY_MAP_REGIONS:
779 case ASM_SESSION_CMD_MEMORY_UNMAP_REGIONS:
780 pr_debug("%s:command[0x%x]success [0x%x]\n",
781 __func__, payload[0], payload[1]);
782 if (atomic_read(&this_mmap.cmd_state)) {
783 atomic_set(&this_mmap.cmd_state, 0);
784 wake_up(&this_mmap.cmd_wait);
785 }
786 break;
787 default:
788 pr_debug("%s:command[0x%x] not expecting rsp\n",
789 __func__, payload[0]);
790 break;
791 }
792 }
793 return 0;
794}
795
796
797static int32_t q6asm_callback(struct apr_client_data *data, void *priv)
798{
799 int i = 0;
800 struct audio_client *ac = (struct audio_client *)priv;
801 uint32_t token;
802 unsigned long dsp_flags;
803 uint32_t *payload;
804
805
806 if ((ac == NULL) || (data == NULL)) {
807 pr_err("ac or priv NULL\n");
808 return -EINVAL;
809 }
810 if (ac->session <= 0 || ac->session > 8) {
811 pr_err("%s:Session ID is invalid, session = %d\n", __func__,
812 ac->session);
813 return -EINVAL;
814 }
815
816 payload = data->payload;
817
818 if (data->opcode == RESET_EVENTS) {
819 pr_debug("q6asm_callback: Reset event is received: %d %d apr[%p]\n",
820 data->reset_event, data->reset_proc, ac->apr);
Laxminath Kasam692c6542012-02-21 11:17:47 +0530821 if (ac->cb)
822 ac->cb(data->opcode, data->token,
823 (uint32_t *)data->payload, ac->priv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700824 apr_reset(ac->apr);
825 return 0;
826 }
827
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700828 pr_debug("%s: session[%d]opcode[0x%x] token[0x%x]payload_s[%d] src[%d] dest[%d]\n",
829 __func__,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700830 ac->session, data->opcode,
831 data->token, data->payload_size, data->src_port,
832 data->dest_port);
833
834 if (data->opcode == APR_BASIC_RSP_RESULT) {
835 token = data->token;
836 switch (payload[0]) {
837 case ASM_STREAM_CMD_SET_PP_PARAMS:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838 if (rtac_make_asm_callback(ac->session, payload,
839 data->payload_size))
840 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700841 case ASM_SESSION_CMD_PAUSE:
842 case ASM_DATA_CMD_EOS:
843 case ASM_STREAM_CMD_CLOSE:
844 case ASM_STREAM_CMD_FLUSH:
845 case ASM_SESSION_CMD_RUN:
846 case ASM_SESSION_CMD_REGISTER_FOR_TX_OVERFLOW_EVENTS:
847 case ASM_STREAM_CMD_FLUSH_READBUFS:
848 pr_debug("%s:Payload = [0x%x]\n", __func__, payload[0]);
849 if (token != ac->session) {
850 pr_err("%s:Invalid session[%d] rxed expected[%d]",
851 __func__, token, ac->session);
852 return -EINVAL;
853 }
854 case ASM_STREAM_CMD_OPEN_READ:
855 case ASM_STREAM_CMD_OPEN_WRITE:
856 case ASM_STREAM_CMD_OPEN_READWRITE:
857 case ASM_DATA_CMD_MEDIA_FORMAT_UPDATE:
858 case ASM_STREAM_CMD_SET_ENCDEC_PARAM:
Santosh Mardi23321202012-03-22 04:33:25 +0530859 case ASM_STREAM_CMD_OPEN_WRITE_COMPRESSED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700860 if (atomic_read(&ac->cmd_state)) {
861 atomic_set(&ac->cmd_state, 0);
862 wake_up(&ac->cmd_wait);
863 }
864 if (ac->cb)
865 ac->cb(data->opcode, data->token,
866 (uint32_t *)data->payload, ac->priv);
867 break;
868 default:
869 pr_debug("%s:command[0x%x] not expecting rsp\n",
870 __func__, payload[0]);
871 break;
872 }
873 return 0;
874 }
875
876 switch (data->opcode) {
877 case ASM_DATA_EVENT_WRITE_DONE:{
878 struct audio_port_data *port = &ac->port[IN];
879 pr_debug("%s: Rxed opcode[0x%x] status[0x%x] token[%d]",
880 __func__, payload[0], payload[1],
881 data->token);
882 if (ac->io_mode == SYNC_IO_MODE) {
883 if (port->buf == NULL) {
884 pr_err("%s: Unexpected Write Done\n",
885 __func__);
886 return -EINVAL;
887 }
888 spin_lock_irqsave(&port->dsp_lock, dsp_flags);
889 if (port->buf[data->token].phys !=
890 payload[0]) {
891 pr_err("Buf expected[%p]rxed[%p]\n",\
892 (void *)port->buf[data->token].phys,\
893 (void *)payload[0]);
894 spin_unlock_irqrestore(&port->dsp_lock,
895 dsp_flags);
896 return -EINVAL;
897 }
898 token = data->token;
899 port->buf[token].used = 1;
900 spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
Rajesha Kini3498c932011-07-19 19:58:27 +0530901#ifdef CONFIG_DEBUG_FS
902 if (out_enable_flag) {
903 /* For first Write done log the time and reset
904 out_cold_index*/
905 if (out_cold_index != 1) {
906 do_gettimeofday(&out_cold_tv);
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700907 pr_debug("COLD: apr_send_pkt at %ld sec %ld microsec\n",
908 out_cold_tv.tv_sec,
Rajesha Kini3498c932011-07-19 19:58:27 +0530909 out_cold_tv.tv_usec);
910 out_cold_index = 1;
911 }
912 pr_debug("out_enable_flag %ld",\
913 out_enable_flag);
914 }
915#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700916 for (i = 0; i < port->max_buf_cnt; i++)
917 pr_debug("%d ", port->buf[i].used);
918
919 }
920 break;
921 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700922 case ASM_STREAM_CMDRSP_GET_PP_PARAMS:
923 rtac_make_asm_callback(ac->session, payload,
924 data->payload_size);
925 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700926 case ASM_DATA_EVENT_READ_DONE:{
927
928 struct audio_port_data *port = &ac->port[OUT];
Rajesha Kini3498c932011-07-19 19:58:27 +0530929#ifdef CONFIG_DEBUG_FS
930 if (in_enable_flag) {
931 /* when in_cont_index == 7, DSP would be
932 * writing into the 8th 512 byte buffer and this
933 * timestamp is tapped here.Once done it then writes
934 * to 9th 512 byte buffer.These two buffers(8th, 9th)
935 * reach the test application in 5th iteration and that
936 * timestamp is tapped at user level. The difference
937 * of these two timestamps gives us the time between
938 * the time at which dsp started filling the sample
939 * required and when it reached the test application.
940 * Hence continuous input latency
941 */
942 if (in_cont_index == 7) {
943 do_gettimeofday(&in_cont_tv);
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700944 pr_err("In_CONT:previous read buffer done at %ld sec %ld microsec\n",
Sriranjan Srikantam74753532011-10-03 14:48:37 -0700945 in_cont_tv.tv_sec, in_cont_tv.tv_usec);
Rajesha Kini3498c932011-07-19 19:58:27 +0530946 }
947 }
948#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700949 pr_debug("%s:R-D: status=%d buff_add=%x act_size=%d offset=%d\n",
950 __func__, payload[READDONE_IDX_STATUS],
951 payload[READDONE_IDX_BUFFER],
952 payload[READDONE_IDX_SIZE],
953 payload[READDONE_IDX_OFFSET]);
954 pr_debug("%s:R-D:msw_ts=%d lsw_ts=%d flags=%d id=%d num=%d\n",
955 __func__, payload[READDONE_IDX_MSW_TS],
956 payload[READDONE_IDX_LSW_TS],
957 payload[READDONE_IDX_FLAGS],
958 payload[READDONE_IDX_ID],
959 payload[READDONE_IDX_NUMFRAMES]);
Rajesha Kini3498c932011-07-19 19:58:27 +0530960#ifdef CONFIG_DEBUG_FS
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700961 if (in_enable_flag)
Rajesha Kini3498c932011-07-19 19:58:27 +0530962 in_cont_index++;
Rajesha Kini3498c932011-07-19 19:58:27 +0530963#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700964 if (ac->io_mode == SYNC_IO_MODE) {
965 if (port->buf == NULL) {
966 pr_err("%s: Unexpected Write Done\n", __func__);
967 return -EINVAL;
968 }
969 spin_lock_irqsave(&port->dsp_lock, dsp_flags);
970 token = data->token;
971 port->buf[token].used = 0;
972 if (port->buf[token].phys !=
973 payload[READDONE_IDX_BUFFER]) {
974 pr_err("Buf expected[%p]rxed[%p]\n",\
975 (void *)port->buf[token].phys,\
976 (void *)payload[READDONE_IDX_BUFFER]);
977 spin_unlock_irqrestore(&port->dsp_lock,
978 dsp_flags);
979 break;
980 }
981 port->buf[token].actual_size =
982 payload[READDONE_IDX_SIZE];
983 spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
984 }
985 break;
986 }
987 case ASM_DATA_EVENT_EOS:
988 case ASM_DATA_CMDRSP_EOS:
989 pr_debug("%s:EOS ACK received: rxed opcode[0x%x]\n",
990 __func__, data->opcode);
991 break;
992 case ASM_STREAM_CMDRSP_GET_ENCDEC_PARAM:
993 break;
994 case ASM_SESSION_EVENT_TX_OVERFLOW:
995 pr_err("ASM_SESSION_EVENT_TX_OVERFLOW\n");
996 break;
997 case ASM_SESSION_CMDRSP_GET_SESSION_TIME:
Harmandeep Singheaf59b42012-06-05 21:46:02 -0700998 pr_debug("%s: ASM_SESSION_CMDRSP_GET_SESSION_TIME, payload[0] = %d, payload[1] = %d, payload[2] = %d\n",
999 __func__,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001000 payload[0], payload[1], payload[2]);
1001 ac->time_stamp = (uint64_t)(((uint64_t)payload[1] << 32) |
1002 payload[2]);
1003 if (atomic_read(&ac->time_flag)) {
1004 atomic_set(&ac->time_flag, 0);
1005 wake_up(&ac->time_wait);
1006 }
1007 break;
1008 case ASM_DATA_EVENT_SR_CM_CHANGE_NOTIFY:
Deepa Madiregama55cbf782011-09-10 05:44:39 +05301009 case ASM_DATA_EVENT_ENC_SR_CM_NOTIFY:
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001010 pr_debug("%s: ASM_DATA_EVENT_SR_CM_CHANGE_NOTIFY, payload[0] = %d, payload[1] = %d, payload[2] = %d, payload[3] = %d\n",
1011 __func__,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001012 payload[0], payload[1], payload[2],
1013 payload[3]);
1014 break;
1015 }
1016 if (ac->cb)
1017 ac->cb(data->opcode, data->token,
1018 data->payload, ac->priv);
1019
1020 return 0;
1021}
1022
1023void *q6asm_is_cpu_buf_avail(int dir, struct audio_client *ac, uint32_t *size,
1024 uint32_t *index)
1025{
1026 void *data;
1027 unsigned char idx;
1028 struct audio_port_data *port;
1029
1030 if (!ac || ((dir != IN) && (dir != OUT)))
1031 return NULL;
1032
1033 if (ac->io_mode == SYNC_IO_MODE) {
1034 port = &ac->port[dir];
1035
1036 mutex_lock(&port->lock);
1037 idx = port->cpu_buf;
1038 if (port->buf == NULL) {
1039 pr_debug("%s:Buffer pointer null\n", __func__);
Jayasena Sangaraboina466e6b92012-01-17 21:10:55 -08001040 mutex_unlock(&port->lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001041 return NULL;
1042 }
1043 /* dir 0: used = 0 means buf in use
1044 dir 1: used = 1 means buf in use */
1045 if (port->buf[idx].used == dir) {
1046 /* To make it more robust, we could loop and get the
1047 next avail buf, its risky though */
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001048 pr_debug("%s:Next buf idx[0x%x] not available,dir[%d]\n",
1049 __func__, idx, dir);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001050 mutex_unlock(&port->lock);
1051 return NULL;
1052 }
1053 *size = port->buf[idx].actual_size;
1054 *index = port->cpu_buf;
1055 data = port->buf[idx].data;
1056 pr_debug("%s:session[%d]index[%d] data[%p]size[%d]\n",
1057 __func__,
1058 ac->session,
1059 port->cpu_buf,
1060 data, *size);
1061 /* By default increase the cpu_buf cnt
1062 user accesses this function,increase cpu
1063 buf(to avoid another api)*/
1064 port->buf[idx].used = dir;
1065 port->cpu_buf = ((port->cpu_buf + 1) & (port->max_buf_cnt - 1));
1066 mutex_unlock(&port->lock);
1067 return data;
1068 }
1069 return NULL;
1070}
1071
Jay Wang9cf59a02011-08-10 16:58:40 -07001072void *q6asm_is_cpu_buf_avail_nolock(int dir, struct audio_client *ac,
1073 uint32_t *size, uint32_t *index)
1074{
1075 void *data;
1076 unsigned char idx;
1077 struct audio_port_data *port;
1078
1079 if (!ac || ((dir != IN) && (dir != OUT)))
1080 return NULL;
1081
1082 port = &ac->port[dir];
1083
1084 idx = port->cpu_buf;
1085 if (port->buf == NULL) {
1086 pr_debug("%s:Buffer pointer null\n", __func__);
1087 return NULL;
1088 }
1089 /*
1090 * dir 0: used = 0 means buf in use
1091 * dir 1: used = 1 means buf in use
1092 */
1093 if (port->buf[idx].used == dir) {
1094 /*
1095 * To make it more robust, we could loop and get the
1096 * next avail buf, its risky though
1097 */
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001098 pr_debug("%s:Next buf idx[0x%x] not available, dir[%d]\n",
1099 __func__, idx, dir);
Jay Wang9cf59a02011-08-10 16:58:40 -07001100 return NULL;
1101 }
1102 *size = port->buf[idx].actual_size;
1103 *index = port->cpu_buf;
1104 data = port->buf[idx].data;
1105 pr_debug("%s:session[%d]index[%d] data[%p]size[%d]\n",
1106 __func__, ac->session, port->cpu_buf,
1107 data, *size);
1108 /*
1109 * By default increase the cpu_buf cnt
1110 * user accesses this function,increase cpu
1111 * buf(to avoid another api)
1112 */
1113 port->buf[idx].used = dir;
1114 port->cpu_buf = ((port->cpu_buf + 1) & (port->max_buf_cnt - 1));
1115 return data;
1116}
1117
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001118int q6asm_is_dsp_buf_avail(int dir, struct audio_client *ac)
1119{
1120 int ret = -1;
1121 struct audio_port_data *port;
1122 uint32_t idx;
1123
1124 if (!ac || (dir != OUT))
1125 return ret;
1126
1127 if (ac->io_mode == SYNC_IO_MODE) {
1128 port = &ac->port[dir];
1129
1130 mutex_lock(&port->lock);
1131 idx = port->dsp_buf;
1132
1133 if (port->buf[idx].used == (dir ^ 1)) {
1134 /* To make it more robust, we could loop and get the
1135 next avail buf, its risky though */
1136 pr_err("Next buf idx[0x%x] not available, dir[%d]\n",
1137 idx, dir);
1138 mutex_unlock(&port->lock);
1139 return ret;
1140 }
1141 pr_debug("%s: session[%d]dsp_buf=%d cpu_buf=%d\n", __func__,
1142 ac->session, port->dsp_buf, port->cpu_buf);
1143 ret = ((port->dsp_buf != port->cpu_buf) ? 0 : -1);
1144 mutex_unlock(&port->lock);
1145 }
1146 return ret;
1147}
1148
1149static void q6asm_add_hdr(struct audio_client *ac, struct apr_hdr *hdr,
1150 uint32_t pkt_size, uint32_t cmd_flg)
1151{
1152 pr_debug("%s:session=%d pkt size=%d cmd_flg=%d\n", __func__, pkt_size,
1153 cmd_flg, ac->session);
1154 mutex_lock(&ac->cmd_lock);
1155 hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, \
1156 APR_HDR_LEN(sizeof(struct apr_hdr)),\
1157 APR_PKT_VER);
1158 hdr->src_svc = ((struct apr_svc *)ac->apr)->id;
1159 hdr->src_domain = APR_DOMAIN_APPS;
1160 hdr->dest_svc = APR_SVC_ASM;
1161 hdr->dest_domain = APR_DOMAIN_ADSP;
1162 hdr->src_port = ((ac->session << 8) & 0xFF00) | 0x01;
1163 hdr->dest_port = ((ac->session << 8) & 0xFF00) | 0x01;
1164 if (cmd_flg) {
1165 hdr->token = ac->session;
1166 atomic_set(&ac->cmd_state, 1);
1167 }
1168 hdr->pkt_size = pkt_size;
1169 mutex_unlock(&ac->cmd_lock);
1170 return;
1171}
1172
1173static void q6asm_add_mmaphdr(struct apr_hdr *hdr, uint32_t pkt_size,
1174 uint32_t cmd_flg)
1175{
1176 pr_debug("%s:pkt size=%d cmd_flg=%d\n", __func__, pkt_size, cmd_flg);
1177 hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, \
1178 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
1179 hdr->src_port = 0;
1180 hdr->dest_port = 0;
1181 if (cmd_flg) {
1182 hdr->token = 0;
1183 atomic_set(&this_mmap.cmd_state, 1);
1184 }
1185 hdr->pkt_size = pkt_size;
1186 return;
1187}
1188
1189int q6asm_open_read(struct audio_client *ac,
1190 uint32_t format)
1191{
1192 int rc = 0x00;
1193 struct asm_stream_cmd_open_read open;
Rajesha Kini3498c932011-07-19 19:58:27 +05301194#ifdef CONFIG_DEBUG_FS
1195 in_cont_index = 0;
1196#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001197 if ((ac == NULL) || (ac->apr == NULL)) {
1198 pr_err("%s: APR handle NULL\n", __func__);
1199 return -EINVAL;
1200 }
1201 pr_debug("%s:session[%d]", __func__, ac->session);
1202
1203 q6asm_add_hdr(ac, &open.hdr, sizeof(open), TRUE);
1204 open.hdr.opcode = ASM_STREAM_CMD_OPEN_READ;
1205 /* Stream prio : High, provide meta info with encoded frames */
1206 open.src_endpoint = ASM_END_POINT_DEVICE_MATRIX;
1207
1208 open.pre_proc_top = get_asm_topology();
1209 if (open.pre_proc_top == 0)
1210 open.pre_proc_top = DEFAULT_POPP_TOPOLOGY;
1211
1212 switch (format) {
1213 case FORMAT_LINEAR_PCM:
1214 open.uMode = STREAM_PRIORITY_HIGH;
1215 open.format = LINEAR_PCM;
1216 break;
Mingming Yin647e9ea2012-03-17 19:56:10 -07001217 case FORMAT_MULTI_CHANNEL_LINEAR_PCM:
1218 open.uMode = STREAM_PRIORITY_HIGH;
1219 open.format = MULTI_CHANNEL_PCM;
1220 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001221 case FORMAT_MPEG4_AAC:
1222 open.uMode = BUFFER_META_ENABLE | STREAM_PRIORITY_HIGH;
1223 open.format = MPEG4_AAC;
1224 break;
1225 case FORMAT_V13K:
1226 open.uMode = BUFFER_META_ENABLE | STREAM_PRIORITY_HIGH;
1227 open.format = V13K_FS;
1228 break;
1229 case FORMAT_EVRC:
1230 open.uMode = BUFFER_META_ENABLE | STREAM_PRIORITY_HIGH;
1231 open.format = EVRC_FS;
1232 break;
1233 case FORMAT_AMRNB:
1234 open.uMode = BUFFER_META_ENABLE | STREAM_PRIORITY_HIGH;
1235 open.format = AMRNB_FS;
1236 break;
Alex Wong2caeecc2011-10-28 10:52:15 +05301237 case FORMAT_AMRWB:
1238 open.uMode = BUFFER_META_ENABLE | STREAM_PRIORITY_HIGH;
1239 open.format = AMRWB_FS;
1240 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001241 default:
1242 pr_err("Invalid format[%d]\n", format);
1243 goto fail_cmd;
1244 }
1245 rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
1246 if (rc < 0) {
1247 pr_err("open failed op[0x%x]rc[%d]\n", \
1248 open.hdr.opcode, rc);
1249 goto fail_cmd;
1250 }
1251 rc = wait_event_timeout(ac->cmd_wait,
1252 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1253 if (!rc) {
1254 pr_err("%s: timeout. waited for OPEN_WRITE rc[%d]\n", __func__,
1255 rc);
1256 goto fail_cmd;
1257 }
1258 return 0;
1259fail_cmd:
1260 return -EINVAL;
1261}
1262
Santosh Mardi23321202012-03-22 04:33:25 +05301263int q6asm_open_write_compressed(struct audio_client *ac, uint32_t format)
1264{
1265 int rc = 0x00;
1266 struct asm_stream_cmd_open_write_compressed open;
1267
1268 if ((ac == NULL) || (ac->apr == NULL)) {
1269 pr_err("%s: APR handle NULL\n", __func__);
1270 return -EINVAL;
1271 }
1272 pr_debug("%s: session[%d] wr_format[0x%x]", __func__, ac->session,
1273 format);
1274
1275 q6asm_add_hdr(ac, &open.hdr, sizeof(open), TRUE);
1276
1277 open.hdr.opcode = ASM_STREAM_CMD_OPEN_WRITE_COMPRESSED;
1278
1279 switch (format) {
1280 case FORMAT_AC3:
1281 open.format = AC3_DECODER;
1282 break;
1283 case FORMAT_EAC3:
1284 open.format = EAC3_DECODER;
1285 break;
1286 case FORMAT_MP3:
1287 open.format = MP3;
1288 break;
1289 case FORMAT_DTS:
1290 open.format = DTS;
1291 break;
1292 case FORMAT_AAC:
1293 open.format = MPEG4_AAC;
1294 break;
1295 case FORMAT_ATRAC:
1296 open.format = ATRAC;
1297 break;
1298 case FORMAT_WMA_V10PRO:
1299 open.format = WMA_V10PRO;
1300 break;
1301 case FORMAT_MAT:
1302 open.format = MAT;
1303 break;
1304 default:
1305 pr_err("%s: Invalid format[%d]\n", __func__, format);
1306 goto fail_cmd;
1307 }
1308 /*Below flag indicates the DSP that Compressed audio input
1309 stream is not IEC 61937 or IEC 60958 packetizied*/
1310 open.flags = 0x00000000;
1311 rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
1312 if (rc < 0) {
1313 pr_err("%s: open failed op[0x%x]rc[%d]\n", \
1314 __func__, open.hdr.opcode, rc);
1315 goto fail_cmd;
1316 }
1317 rc = wait_event_timeout(ac->cmd_wait,
1318 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1319 if (!rc) {
1320 pr_err("%s: timeout. waited for OPEN_WRITE rc[%d]\n", __func__,
1321 rc);
1322 goto fail_cmd;
1323 }
1324 return 0;
1325fail_cmd:
1326 return -EINVAL;
1327}
1328
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001329int q6asm_open_write(struct audio_client *ac, uint32_t format)
1330{
1331 int rc = 0x00;
1332 struct asm_stream_cmd_open_write open;
1333
1334 if ((ac == NULL) || (ac->apr == NULL)) {
1335 pr_err("%s: APR handle NULL\n", __func__);
1336 return -EINVAL;
1337 }
1338 pr_debug("%s: session[%d] wr_format[0x%x]", __func__, ac->session,
1339 format);
1340
1341 q6asm_add_hdr(ac, &open.hdr, sizeof(open), TRUE);
1342
1343 open.hdr.opcode = ASM_STREAM_CMD_OPEN_WRITE;
1344 open.uMode = STREAM_PRIORITY_HIGH;
1345 /* source endpoint : matrix */
1346 open.sink_endpoint = ASM_END_POINT_DEVICE_MATRIX;
1347 open.stream_handle = 0x00;
1348
1349 open.post_proc_top = get_asm_topology();
1350 if (open.post_proc_top == 0)
1351 open.post_proc_top = DEFAULT_POPP_TOPOLOGY;
1352
1353 switch (format) {
1354 case FORMAT_LINEAR_PCM:
1355 open.format = LINEAR_PCM;
1356 break;
Kiran Kandi5e809b02012-01-31 00:24:33 -08001357 case FORMAT_MULTI_CHANNEL_LINEAR_PCM:
1358 open.format = MULTI_CHANNEL_PCM;
1359 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001360 case FORMAT_MPEG4_AAC:
1361 open.format = MPEG4_AAC;
1362 break;
Bharath Ramachandramurthy4f71d502011-10-23 19:45:22 -07001363 case FORMAT_MPEG4_MULTI_AAC:
1364 open.format = MPEG4_MULTI_AAC;
1365 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001366 case FORMAT_WMA_V9:
1367 open.format = WMA_V9;
1368 break;
1369 case FORMAT_WMA_V10PRO:
1370 open.format = WMA_V10PRO;
1371 break;
Asish Bhattacharya305d1752011-11-01 20:38:26 +05301372 case FORMAT_MP3:
1373 open.format = MP3;
1374 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001375 default:
1376 pr_err("%s: Invalid format[%d]\n", __func__, format);
1377 goto fail_cmd;
1378 }
1379 rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
1380 if (rc < 0) {
1381 pr_err("%s: open failed op[0x%x]rc[%d]\n", \
1382 __func__, open.hdr.opcode, rc);
1383 goto fail_cmd;
1384 }
1385 rc = wait_event_timeout(ac->cmd_wait,
1386 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1387 if (!rc) {
1388 pr_err("%s: timeout. waited for OPEN_WRITE rc[%d]\n", __func__,
1389 rc);
1390 goto fail_cmd;
1391 }
1392 return 0;
1393fail_cmd:
1394 return -EINVAL;
1395}
1396
1397int q6asm_open_read_write(struct audio_client *ac,
1398 uint32_t rd_format,
1399 uint32_t wr_format)
1400{
1401 int rc = 0x00;
1402 struct asm_stream_cmd_open_read_write open;
1403
1404 if ((ac == NULL) || (ac->apr == NULL)) {
1405 pr_err("APR handle NULL\n");
1406 return -EINVAL;
1407 }
1408 pr_debug("%s: session[%d]", __func__, ac->session);
1409 pr_debug("wr_format[0x%x]rd_format[0x%x]",
1410 wr_format, rd_format);
1411
1412 q6asm_add_hdr(ac, &open.hdr, sizeof(open), TRUE);
1413 open.hdr.opcode = ASM_STREAM_CMD_OPEN_READWRITE;
1414
Deepa Madiregama55cbf782011-09-10 05:44:39 +05301415 open.uMode = BUFFER_META_ENABLE | STREAM_PRIORITY_NORMAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001416 /* source endpoint : matrix */
1417 open.post_proc_top = get_asm_topology();
1418 if (open.post_proc_top == 0)
1419 open.post_proc_top = DEFAULT_POPP_TOPOLOGY;
1420
1421 switch (wr_format) {
1422 case FORMAT_LINEAR_PCM:
1423 open.write_format = LINEAR_PCM;
1424 break;
1425 case FORMAT_MPEG4_AAC:
1426 open.write_format = MPEG4_AAC;
1427 break;
Bharath Ramachandramurthy4f71d502011-10-23 19:45:22 -07001428 case FORMAT_MPEG4_MULTI_AAC:
1429 open.write_format = MPEG4_MULTI_AAC;
1430 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001431 case FORMAT_WMA_V9:
1432 open.write_format = WMA_V9;
1433 break;
1434 case FORMAT_WMA_V10PRO:
1435 open.write_format = WMA_V10PRO;
1436 break;
Alex Wong2caeecc2011-10-28 10:52:15 +05301437 case FORMAT_AMRNB:
1438 open.write_format = AMRNB_FS;
1439 break;
1440 case FORMAT_AMRWB:
1441 open.write_format = AMRWB_FS;
1442 break;
1443 case FORMAT_V13K:
1444 open.write_format = V13K_FS;
1445 break;
1446 case FORMAT_EVRC:
1447 open.write_format = EVRC_FS;
1448 break;
1449 case FORMAT_EVRCB:
1450 open.write_format = EVRCB_FS;
1451 break;
1452 case FORMAT_EVRCWB:
1453 open.write_format = EVRCWB_FS;
1454 break;
1455 case FORMAT_MP3:
1456 open.write_format = MP3;
1457 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001458 default:
1459 pr_err("Invalid format[%d]\n", wr_format);
1460 goto fail_cmd;
1461 }
1462
1463 switch (rd_format) {
1464 case FORMAT_LINEAR_PCM:
1465 open.read_format = LINEAR_PCM;
1466 break;
1467 case FORMAT_MPEG4_AAC:
1468 open.read_format = MPEG4_AAC;
1469 break;
1470 case FORMAT_V13K:
1471 open.read_format = V13K_FS;
1472 break;
1473 case FORMAT_EVRC:
1474 open.read_format = EVRC_FS;
1475 break;
1476 case FORMAT_AMRNB:
1477 open.read_format = AMRNB_FS;
1478 break;
Alex Wong2caeecc2011-10-28 10:52:15 +05301479 case FORMAT_AMRWB:
1480 open.read_format = AMRWB_FS;
1481 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001482 default:
1483 pr_err("Invalid format[%d]\n", rd_format);
1484 goto fail_cmd;
1485 }
1486 pr_debug("%s:rdformat[0x%x]wrformat[0x%x]\n", __func__,
1487 open.read_format, open.write_format);
1488
1489 rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
1490 if (rc < 0) {
1491 pr_err("open failed op[0x%x]rc[%d]\n", \
1492 open.hdr.opcode, rc);
1493 goto fail_cmd;
1494 }
1495 rc = wait_event_timeout(ac->cmd_wait,
1496 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1497 if (!rc) {
1498 pr_err("timeout. waited for OPEN_WRITE rc[%d]\n", rc);
1499 goto fail_cmd;
1500 }
1501 return 0;
1502fail_cmd:
1503 return -EINVAL;
1504}
1505
1506int q6asm_run(struct audio_client *ac, uint32_t flags,
1507 uint32_t msw_ts, uint32_t lsw_ts)
1508{
1509 struct asm_stream_cmd_run run;
1510 int rc;
1511 if (!ac || ac->apr == NULL) {
1512 pr_err("APR handle NULL\n");
1513 return -EINVAL;
1514 }
1515 pr_debug("%s session[%d]", __func__, ac->session);
1516 q6asm_add_hdr(ac, &run.hdr, sizeof(run), TRUE);
1517
1518 run.hdr.opcode = ASM_SESSION_CMD_RUN;
1519 run.flags = flags;
1520 run.msw_ts = msw_ts;
1521 run.lsw_ts = lsw_ts;
Rajesha Kini3498c932011-07-19 19:58:27 +05301522#ifdef CONFIG_DEBUG_FS
1523 if (out_enable_flag) {
1524 do_gettimeofday(&out_cold_tv);
1525 pr_debug("COLD: apr_send_pkt at %ld sec %ld microsec\n",\
1526 out_cold_tv.tv_sec, out_cold_tv.tv_usec);
1527 }
1528#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001529 rc = apr_send_pkt(ac->apr, (uint32_t *) &run);
1530 if (rc < 0) {
1531 pr_err("Commmand run failed[%d]", rc);
1532 goto fail_cmd;
1533 }
1534
1535 rc = wait_event_timeout(ac->cmd_wait,
1536 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1537 if (!rc) {
1538 pr_err("timeout. waited for run success rc[%d]", rc);
1539 goto fail_cmd;
1540 }
1541
1542 return 0;
1543fail_cmd:
1544 return -EINVAL;
1545}
1546
1547int q6asm_run_nowait(struct audio_client *ac, uint32_t flags,
1548 uint32_t msw_ts, uint32_t lsw_ts)
1549{
1550 struct asm_stream_cmd_run run;
1551 int rc;
1552 if (!ac || ac->apr == NULL) {
1553 pr_err("%s:APR handle NULL\n", __func__);
1554 return -EINVAL;
1555 }
1556 pr_debug("session[%d]", ac->session);
1557 q6asm_add_hdr_async(ac, &run.hdr, sizeof(run), TRUE);
1558
1559 run.hdr.opcode = ASM_SESSION_CMD_RUN;
1560 run.flags = flags;
1561 run.msw_ts = msw_ts;
1562 run.lsw_ts = lsw_ts;
1563
1564 rc = apr_send_pkt(ac->apr, (uint32_t *) &run);
1565 if (rc < 0) {
1566 pr_err("%s:Commmand run failed[%d]", __func__, rc);
1567 return -EINVAL;
1568 }
1569 return 0;
1570}
1571
1572
1573int q6asm_enc_cfg_blk_aac(struct audio_client *ac,
1574 uint32_t frames_per_buf,
1575 uint32_t sample_rate, uint32_t channels,
1576 uint32_t bit_rate, uint32_t mode, uint32_t format)
1577{
1578 struct asm_stream_cmd_encdec_cfg_blk enc_cfg;
1579 int rc = 0;
1580
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001581 pr_debug("%s:session[%d]frames[%d]SR[%d]ch[%d]bitrate[%d]mode[%d] format[%d]",
1582 __func__, ac->session, frames_per_buf,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001583 sample_rate, channels, bit_rate, mode, format);
1584
1585 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
1586
1587 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1588 enc_cfg.param_id = ASM_ENCDEC_CFG_BLK_ID;
1589 enc_cfg.param_size = sizeof(struct asm_encode_cfg_blk);
1590 enc_cfg.enc_blk.frames_per_buf = frames_per_buf;
1591 enc_cfg.enc_blk.format_id = MPEG4_AAC;
1592 enc_cfg.enc_blk.cfg_size = sizeof(struct asm_aac_read_cfg);
1593 enc_cfg.enc_blk.cfg.aac.bitrate = bit_rate;
1594 enc_cfg.enc_blk.cfg.aac.enc_mode = mode;
1595 enc_cfg.enc_blk.cfg.aac.format = format;
1596 enc_cfg.enc_blk.cfg.aac.ch_cfg = channels;
1597 enc_cfg.enc_blk.cfg.aac.sample_rate = sample_rate;
1598
1599 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
1600 if (rc < 0) {
1601 pr_err("Comamnd %d failed\n", ASM_STREAM_CMD_SET_ENCDEC_PARAM);
1602 rc = -EINVAL;
1603 goto fail_cmd;
1604 }
1605 rc = wait_event_timeout(ac->cmd_wait,
1606 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1607 if (!rc) {
1608 pr_err("timeout. waited for FORMAT_UPDATE\n");
1609 goto fail_cmd;
1610 }
1611 return 0;
1612fail_cmd:
1613 return -EINVAL;
1614}
1615
1616int q6asm_enc_cfg_blk_pcm(struct audio_client *ac,
1617 uint32_t rate, uint32_t channels)
1618{
1619 struct asm_stream_cmd_encdec_cfg_blk enc_cfg;
1620
1621 int rc = 0;
1622
1623 pr_debug("%s: Session %d, rate = %d, channels = %d\n", __func__,
1624 ac->session, rate, channels);
1625
1626 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
1627
1628 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1629 enc_cfg.param_id = ASM_ENCDEC_CFG_BLK_ID;
1630 enc_cfg.param_size = sizeof(struct asm_encode_cfg_blk);
1631 enc_cfg.enc_blk.frames_per_buf = 1;
1632 enc_cfg.enc_blk.format_id = LINEAR_PCM;
1633 enc_cfg.enc_blk.cfg_size = sizeof(struct asm_pcm_cfg);
1634 enc_cfg.enc_blk.cfg.pcm.ch_cfg = channels;
1635 enc_cfg.enc_blk.cfg.pcm.bits_per_sample = 16;
1636 enc_cfg.enc_blk.cfg.pcm.sample_rate = rate;
1637 enc_cfg.enc_blk.cfg.pcm.is_signed = 1;
1638 enc_cfg.enc_blk.cfg.pcm.interleaved = 1;
1639
1640 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
1641 if (rc < 0) {
1642 pr_err("Comamnd open failed\n");
1643 rc = -EINVAL;
1644 goto fail_cmd;
1645 }
1646 rc = wait_event_timeout(ac->cmd_wait,
1647 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1648 if (!rc) {
1649 pr_err("timeout opcode[0x%x] ", enc_cfg.hdr.opcode);
1650 goto fail_cmd;
1651 }
1652 return 0;
1653fail_cmd:
1654 return -EINVAL;
1655}
1656
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001657int q6asm_enc_cfg_blk_pcm_native(struct audio_client *ac,
1658 uint32_t rate, uint32_t channels)
1659{
1660 struct asm_stream_cmd_encdec_cfg_blk enc_cfg;
1661
1662 int rc = 0;
1663
1664 pr_debug("%s: Session %d, rate = %d, channels = %d, setting the rate and channels to 0 for native\n",
1665 __func__, ac->session, rate, channels);
1666
1667 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
1668
1669 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1670 enc_cfg.param_id = ASM_ENCDEC_CFG_BLK_ID;
1671 enc_cfg.param_size = sizeof(struct asm_encode_cfg_blk);
1672 enc_cfg.enc_blk.frames_per_buf = 1;
1673 enc_cfg.enc_blk.format_id = LINEAR_PCM;
1674 enc_cfg.enc_blk.cfg_size = sizeof(struct asm_pcm_cfg);
1675 enc_cfg.enc_blk.cfg.pcm.ch_cfg = 0;/*channels;*/
1676 enc_cfg.enc_blk.cfg.pcm.bits_per_sample = 16;
1677 enc_cfg.enc_blk.cfg.pcm.sample_rate = 0;/*rate;*/
1678 enc_cfg.enc_blk.cfg.pcm.is_signed = 1;
1679 enc_cfg.enc_blk.cfg.pcm.interleaved = 1;
1680
1681 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
1682 if (rc < 0) {
1683 pr_err("Comamnd open failed\n");
1684 rc = -EINVAL;
1685 goto fail_cmd;
1686 }
1687 rc = wait_event_timeout(ac->cmd_wait,
1688 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1689 if (!rc) {
1690 pr_err("timeout opcode[0x%x] ", enc_cfg.hdr.opcode);
1691 goto fail_cmd;
1692 }
1693 return 0;
1694fail_cmd:
1695 return -EINVAL;
1696}
1697
Mingming Yin647e9ea2012-03-17 19:56:10 -07001698int q6asm_enc_cfg_blk_multi_ch_pcm(struct audio_client *ac,
1699 uint32_t rate, uint32_t channels)
1700{
1701 struct asm_stream_cmd_encdec_cfg_blk enc_cfg;
1702
1703 int rc = 0;
1704
1705 pr_debug("%s: Session %d, rate = %d, channels = %d\n", __func__,
1706 ac->session, rate, channels);
1707
1708 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
1709
1710 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1711 enc_cfg.param_id = ASM_ENCDEC_CFG_BLK_ID;
1712 enc_cfg.param_size = sizeof(struct asm_encode_cfg_blk);
1713 enc_cfg.enc_blk.frames_per_buf = 1;
1714 enc_cfg.enc_blk.format_id = MULTI_CHANNEL_PCM;
1715 enc_cfg.enc_blk.cfg_size =
1716 sizeof(struct asm_multi_channel_pcm_fmt_blk);
1717 enc_cfg.enc_blk.cfg.mpcm.num_channels = channels;
1718 enc_cfg.enc_blk.cfg.mpcm.bits_per_sample = 16;
1719 enc_cfg.enc_blk.cfg.mpcm.sample_rate = rate;
1720 enc_cfg.enc_blk.cfg.mpcm.is_signed = 1;
1721 enc_cfg.enc_blk.cfg.mpcm.is_interleaved = 1;
1722 enc_cfg.enc_blk.cfg.mpcm.channel_mapping[0] = PCM_CHANNEL_FL;
1723 enc_cfg.enc_blk.cfg.mpcm.channel_mapping[1] = PCM_CHANNEL_FR;
1724 enc_cfg.enc_blk.cfg.mpcm.channel_mapping[2] = PCM_CHANNEL_RB;
1725 enc_cfg.enc_blk.cfg.mpcm.channel_mapping[3] = PCM_CHANNEL_LB;
1726 enc_cfg.enc_blk.cfg.mpcm.channel_mapping[4] = 0;
1727 enc_cfg.enc_blk.cfg.mpcm.channel_mapping[5] = 0;
1728 enc_cfg.enc_blk.cfg.mpcm.channel_mapping[6] = 0;
1729 enc_cfg.enc_blk.cfg.mpcm.channel_mapping[7] = 0;
1730
1731 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
1732 if (rc < 0) {
1733 pr_err("Comamnd open failed\n");
1734 rc = -EINVAL;
1735 goto fail_cmd;
1736 }
1737 rc = wait_event_timeout(ac->cmd_wait,
1738 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1739 if (!rc) {
1740 pr_err("timeout opcode[0x%x] ", enc_cfg.hdr.opcode);
1741 goto fail_cmd;
1742 }
1743 return 0;
1744fail_cmd:
1745 return -EINVAL;
1746}
1747
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001748int q6asm_enable_sbrps(struct audio_client *ac,
1749 uint32_t sbr_ps_enable)
1750{
1751 struct asm_stream_cmd_encdec_sbr sbrps;
1752
1753 int rc = 0;
1754
1755 pr_debug("%s: Session %d\n", __func__, ac->session);
1756
1757 q6asm_add_hdr(ac, &sbrps.hdr, sizeof(sbrps), TRUE);
1758
1759 sbrps.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1760 sbrps.param_id = ASM_ENABLE_SBR_PS;
1761 sbrps.param_size = sizeof(struct asm_sbr_ps);
1762 sbrps.sbr_ps.enable = sbr_ps_enable;
1763
1764 rc = apr_send_pkt(ac->apr, (uint32_t *) &sbrps);
1765 if (rc < 0) {
1766 pr_err("Command opcode[0x%x]paramid[0x%x] failed\n",
1767 ASM_STREAM_CMD_SET_ENCDEC_PARAM,
1768 ASM_ENABLE_SBR_PS);
1769 rc = -EINVAL;
1770 goto fail_cmd;
1771 }
1772 rc = wait_event_timeout(ac->cmd_wait,
1773 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1774 if (!rc) {
1775 pr_err("timeout opcode[0x%x] ", sbrps.hdr.opcode);
1776 goto fail_cmd;
1777 }
1778 return 0;
1779fail_cmd:
1780 return -EINVAL;
1781}
1782
Swaminathan Sathappan70765cd2011-07-19 18:42:47 -07001783int q6asm_cfg_dual_mono_aac(struct audio_client *ac,
1784 uint16_t sce_left, uint16_t sce_right)
1785{
1786 struct asm_stream_cmd_encdec_dualmono dual_mono;
1787
1788 int rc = 0;
1789
1790 pr_debug("%s: Session %d, sce_left = %d, sce_right = %d\n",
1791 __func__, ac->session, sce_left, sce_right);
1792
1793 q6asm_add_hdr(ac, &dual_mono.hdr, sizeof(dual_mono), TRUE);
1794
1795 dual_mono.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1796 dual_mono.param_id = ASM_CONFIGURE_DUAL_MONO;
1797 dual_mono.param_size = sizeof(struct asm_dual_mono);
1798 dual_mono.channel_map.sce_left = sce_left;
1799 dual_mono.channel_map.sce_right = sce_right;
1800
1801 rc = apr_send_pkt(ac->apr, (uint32_t *) &dual_mono);
1802 if (rc < 0) {
1803 pr_err("%s:Command opcode[0x%x]paramid[0x%x] failed\n",
1804 __func__, ASM_STREAM_CMD_SET_ENCDEC_PARAM,
1805 ASM_CONFIGURE_DUAL_MONO);
1806 rc = -EINVAL;
1807 goto fail_cmd;
1808 }
1809 rc = wait_event_timeout(ac->cmd_wait,
1810 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1811 if (!rc) {
1812 pr_err("%s:timeout opcode[0x%x]\n", __func__,
1813 dual_mono.hdr.opcode);
1814 goto fail_cmd;
1815 }
1816 return 0;
1817fail_cmd:
1818 return -EINVAL;
1819}
1820
Swaminathan Sathappan6f530882012-05-01 16:42:22 -07001821int q6asm_set_encdec_chan_map(struct audio_client *ac,
1822 uint32_t num_channels)
1823{
1824 struct asm_stream_cmd_encdec_channelmap chan_map;
1825 u8 *channel_mapping;
1826
1827 int rc = 0;
1828
1829 pr_debug("%s: Session %d, num_channels = %d\n",
1830 __func__, ac->session, num_channels);
1831
1832 q6asm_add_hdr(ac, &chan_map.hdr, sizeof(chan_map), TRUE);
1833
1834 chan_map.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1835 chan_map.param_id = ASM_ENCDEC_DEC_CHAN_MAP;
1836 chan_map.param_size = sizeof(struct asm_dec_chan_map);
1837 chan_map.chan_map.num_channels = num_channels;
1838
1839 channel_mapping =
1840 chan_map.chan_map.channel_mapping;
1841
1842 memset(channel_mapping, PCM_CHANNEL_NULL, MAX_CHAN_MAP_CHANNELS);
1843 if (num_channels == 1) {
1844 channel_mapping[0] = PCM_CHANNEL_FL;
1845 } else if (num_channels == 2) {
1846 channel_mapping[0] = PCM_CHANNEL_FL;
1847 channel_mapping[1] = PCM_CHANNEL_FR;
1848 } else if (num_channels == 6) {
1849 channel_mapping[0] = PCM_CHANNEL_FC;
1850 channel_mapping[1] = PCM_CHANNEL_FL;
1851 channel_mapping[2] = PCM_CHANNEL_FR;
1852 channel_mapping[3] = PCM_CHANNEL_LB;
1853 channel_mapping[4] = PCM_CHANNEL_RB;
1854 channel_mapping[5] = PCM_CHANNEL_LFE;
1855 } else {
1856 pr_err("%s: ERROR.unsupported num_ch = %u\n", __func__,
1857 num_channels);
1858 rc = -EINVAL;
1859 goto fail_cmd;
1860 }
1861
1862 rc = apr_send_pkt(ac->apr, (uint32_t *) &chan_map);
1863 if (rc < 0) {
1864 pr_err("%s:Command opcode[0x%x]paramid[0x%x] failed\n",
1865 __func__, ASM_STREAM_CMD_SET_ENCDEC_PARAM,
1866 ASM_ENCDEC_DEC_CHAN_MAP);
1867 goto fail_cmd;
1868 }
1869 rc = wait_event_timeout(ac->cmd_wait,
1870 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1871 if (!rc) {
1872 pr_err("%s:timeout opcode[0x%x]\n", __func__,
1873 chan_map.hdr.opcode);
1874 rc = -ETIMEDOUT;
1875 goto fail_cmd;
1876 }
1877 return 0;
1878fail_cmd:
1879 return rc;
1880}
1881
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001882int q6asm_enc_cfg_blk_qcelp(struct audio_client *ac, uint32_t frames_per_buf,
1883 uint16_t min_rate, uint16_t max_rate,
1884 uint16_t reduced_rate_level, uint16_t rate_modulation_cmd)
1885{
1886 struct asm_stream_cmd_encdec_cfg_blk enc_cfg;
1887 int rc = 0;
1888
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001889 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]",
1890 __func__,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001891 ac->session, frames_per_buf, min_rate, max_rate,
1892 reduced_rate_level, rate_modulation_cmd);
1893
1894 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
1895
1896 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1897
1898 enc_cfg.param_id = ASM_ENCDEC_CFG_BLK_ID;
1899 enc_cfg.param_size = sizeof(struct asm_encode_cfg_blk);
1900
1901 enc_cfg.enc_blk.frames_per_buf = frames_per_buf;
1902 enc_cfg.enc_blk.format_id = V13K_FS;
1903 enc_cfg.enc_blk.cfg_size = sizeof(struct asm_qcelp13_read_cfg);
1904 enc_cfg.enc_blk.cfg.qcelp13.min_rate = min_rate;
1905 enc_cfg.enc_blk.cfg.qcelp13.max_rate = max_rate;
1906 enc_cfg.enc_blk.cfg.qcelp13.reduced_rate_level = reduced_rate_level;
1907 enc_cfg.enc_blk.cfg.qcelp13.rate_modulation_cmd = rate_modulation_cmd;
1908
1909 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
1910 if (rc < 0) {
1911 pr_err("Comamnd %d failed\n", ASM_STREAM_CMD_SET_ENCDEC_PARAM);
1912 goto fail_cmd;
1913 }
1914 rc = wait_event_timeout(ac->cmd_wait,
1915 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1916 if (!rc) {
1917 pr_err("timeout. waited for FORMAT_UPDATE\n");
1918 goto fail_cmd;
1919 }
1920 return 0;
1921fail_cmd:
1922 return -EINVAL;
1923}
1924
1925int q6asm_enc_cfg_blk_evrc(struct audio_client *ac, uint32_t frames_per_buf,
1926 uint16_t min_rate, uint16_t max_rate,
1927 uint16_t rate_modulation_cmd)
1928{
1929 struct asm_stream_cmd_encdec_cfg_blk enc_cfg;
1930 int rc = 0;
1931
Harmandeep Singheaf59b42012-06-05 21:46:02 -07001932 pr_debug("%s:session[%d]frames[%d]min_rate[0x%4x]max_rate[0x%4x] rate_modulation_cmd[0x%4x]",
1933 __func__, ac->session,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001934 frames_per_buf, min_rate, max_rate, rate_modulation_cmd);
1935
1936 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
1937
1938 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1939
1940 enc_cfg.param_id = ASM_ENCDEC_CFG_BLK_ID;
1941 enc_cfg.param_size = sizeof(struct asm_encode_cfg_blk);
1942
1943 enc_cfg.enc_blk.frames_per_buf = frames_per_buf;
1944 enc_cfg.enc_blk.format_id = EVRC_FS;
1945 enc_cfg.enc_blk.cfg_size = sizeof(struct asm_evrc_read_cfg);
1946 enc_cfg.enc_blk.cfg.evrc.min_rate = min_rate;
1947 enc_cfg.enc_blk.cfg.evrc.max_rate = max_rate;
1948 enc_cfg.enc_blk.cfg.evrc.rate_modulation_cmd = rate_modulation_cmd;
1949 enc_cfg.enc_blk.cfg.evrc.reserved = 0;
1950
1951 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
1952 if (rc < 0) {
1953 pr_err("Comamnd %d failed\n", ASM_STREAM_CMD_SET_ENCDEC_PARAM);
1954 goto fail_cmd;
1955 }
1956 rc = wait_event_timeout(ac->cmd_wait,
1957 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1958 if (!rc) {
1959 pr_err("timeout. waited for FORMAT_UPDATE\n");
1960 goto fail_cmd;
1961 }
1962 return 0;
1963fail_cmd:
1964 return -EINVAL;
1965}
1966
1967int q6asm_enc_cfg_blk_amrnb(struct audio_client *ac, uint32_t frames_per_buf,
1968 uint16_t band_mode, uint16_t dtx_enable)
1969{
1970 struct asm_stream_cmd_encdec_cfg_blk enc_cfg;
1971 int rc = 0;
1972
1973 pr_debug("%s:session[%d]frames[%d]band_mode[0x%4x]dtx_enable[0x%4x]",
1974 __func__, ac->session, frames_per_buf, band_mode, dtx_enable);
1975
1976 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
1977
1978 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
1979
1980 enc_cfg.param_id = ASM_ENCDEC_CFG_BLK_ID;
1981 enc_cfg.param_size = sizeof(struct asm_encode_cfg_blk);
1982
1983 enc_cfg.enc_blk.frames_per_buf = frames_per_buf;
1984 enc_cfg.enc_blk.format_id = AMRNB_FS;
1985 enc_cfg.enc_blk.cfg_size = sizeof(struct asm_amrnb_read_cfg);
1986 enc_cfg.enc_blk.cfg.amrnb.mode = band_mode;
1987 enc_cfg.enc_blk.cfg.amrnb.dtx_mode = dtx_enable;
1988
1989 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
1990 if (rc < 0) {
1991 pr_err("Comamnd %d failed\n", ASM_STREAM_CMD_SET_ENCDEC_PARAM);
1992 goto fail_cmd;
1993 }
1994 rc = wait_event_timeout(ac->cmd_wait,
1995 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
1996 if (!rc) {
1997 pr_err("timeout. waited for FORMAT_UPDATE\n");
1998 goto fail_cmd;
1999 }
2000 return 0;
2001fail_cmd:
2002 return -EINVAL;
2003}
2004
Alex Wong2caeecc2011-10-28 10:52:15 +05302005int q6asm_enc_cfg_blk_amrwb(struct audio_client *ac, uint32_t frames_per_buf,
2006 uint16_t band_mode, uint16_t dtx_enable)
2007{
2008 struct asm_stream_cmd_encdec_cfg_blk enc_cfg;
2009 int rc = 0;
2010
2011 pr_debug("%s:session[%d]frames[%d]band_mode[0x%4x]dtx_enable[0x%4x]",
2012 __func__, ac->session, frames_per_buf, band_mode, dtx_enable);
2013
2014 q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
2015
2016 enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
2017
2018 enc_cfg.param_id = ASM_ENCDEC_CFG_BLK_ID;
2019 enc_cfg.param_size = sizeof(struct asm_encode_cfg_blk);
2020
2021 enc_cfg.enc_blk.frames_per_buf = frames_per_buf;
2022 enc_cfg.enc_blk.format_id = AMRWB_FS;
2023 enc_cfg.enc_blk.cfg_size = sizeof(struct asm_amrwb_read_cfg);
2024 enc_cfg.enc_blk.cfg.amrwb.mode = band_mode;
2025 enc_cfg.enc_blk.cfg.amrwb.dtx_mode = dtx_enable;
2026
2027 rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
2028 if (rc < 0) {
2029 pr_err("Comamnd %d failed\n", ASM_STREAM_CMD_SET_ENCDEC_PARAM);
2030 goto fail_cmd;
2031 }
2032 rc = wait_event_timeout(ac->cmd_wait,
2033 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2034 if (!rc) {
2035 pr_err("timeout. waited for FORMAT_UPDATE\n");
2036 goto fail_cmd;
2037 }
2038 return 0;
2039fail_cmd:
2040 return -EINVAL;
2041}
2042
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002043int q6asm_media_format_block_pcm(struct audio_client *ac,
2044 uint32_t rate, uint32_t channels)
2045{
2046 struct asm_stream_media_format_update fmt;
2047 int rc = 0;
2048
2049 pr_debug("%s:session[%d]rate[%d]ch[%d]\n", __func__, ac->session, rate,
2050 channels);
2051
2052 q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
2053
2054 fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FORMAT_UPDATE;
2055
2056 fmt.format = LINEAR_PCM;
2057 fmt.cfg_size = sizeof(struct asm_pcm_cfg);
2058 fmt.write_cfg.pcm_cfg.ch_cfg = channels;
2059 fmt.write_cfg.pcm_cfg.bits_per_sample = 16;
2060 fmt.write_cfg.pcm_cfg.sample_rate = rate;
2061 fmt.write_cfg.pcm_cfg.is_signed = 1;
2062 fmt.write_cfg.pcm_cfg.interleaved = 1;
2063
2064 rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
2065 if (rc < 0) {
2066 pr_err("%s:Comamnd open failed\n", __func__);
2067 goto fail_cmd;
2068 }
2069 rc = wait_event_timeout(ac->cmd_wait,
2070 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2071 if (!rc) {
2072 pr_err("%s:timeout. waited for FORMAT_UPDATE\n", __func__);
2073 goto fail_cmd;
2074 }
2075 return 0;
2076fail_cmd:
2077 return -EINVAL;
2078}
2079
Kiran Kandi5e809b02012-01-31 00:24:33 -08002080int q6asm_media_format_block_multi_ch_pcm(struct audio_client *ac,
2081 uint32_t rate, uint32_t channels)
2082{
2083 struct asm_stream_media_format_update fmt;
2084 u8 *channel_mapping;
2085 int rc = 0;
2086
2087 pr_debug("%s:session[%d]rate[%d]ch[%d]\n", __func__, ac->session, rate,
2088 channels);
2089
2090 q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
2091
2092 fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FORMAT_UPDATE;
2093
2094 fmt.format = MULTI_CHANNEL_PCM;
2095 fmt.cfg_size = sizeof(struct asm_multi_channel_pcm_fmt_blk);
2096 fmt.write_cfg.multi_ch_pcm_cfg.num_channels = channels;
2097 fmt.write_cfg.multi_ch_pcm_cfg.bits_per_sample = 16;
2098 fmt.write_cfg.multi_ch_pcm_cfg.sample_rate = rate;
2099 fmt.write_cfg.multi_ch_pcm_cfg.is_signed = 1;
2100 fmt.write_cfg.multi_ch_pcm_cfg.is_interleaved = 1;
2101 channel_mapping =
2102 fmt.write_cfg.multi_ch_pcm_cfg.channel_mapping;
2103
2104 memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
2105
2106 if (channels == 1) {
2107 channel_mapping[0] = PCM_CHANNEL_FL;
2108 } else if (channels == 2) {
2109 channel_mapping[0] = PCM_CHANNEL_FL;
2110 channel_mapping[1] = PCM_CHANNEL_FR;
2111 } else if (channels == 6) {
2112 channel_mapping[0] = PCM_CHANNEL_FC;
2113 channel_mapping[1] = PCM_CHANNEL_FL;
2114 channel_mapping[2] = PCM_CHANNEL_FR;
2115 channel_mapping[3] = PCM_CHANNEL_LB;
2116 channel_mapping[4] = PCM_CHANNEL_RB;
2117 channel_mapping[5] = PCM_CHANNEL_LFE;
2118 } else {
2119 pr_err("%s: ERROR.unsupported num_ch = %u\n", __func__,
2120 channels);
2121 return -EINVAL;
2122 }
2123
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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002140int q6asm_media_format_block_aac(struct audio_client *ac,
2141 struct asm_aac_cfg *cfg)
2142{
2143 struct asm_stream_media_format_update fmt;
2144 int rc = 0;
2145
2146 pr_debug("%s:session[%d]rate[%d]ch[%d]\n", __func__, ac->session,
2147 cfg->sample_rate, cfg->ch_cfg);
2148
2149 q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
2150
2151 fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FORMAT_UPDATE;
2152
2153 fmt.format = MPEG4_AAC;
2154 fmt.cfg_size = sizeof(struct asm_aac_cfg);
2155 fmt.write_cfg.aac_cfg.format = cfg->format;
2156 fmt.write_cfg.aac_cfg.aot = cfg->aot;
2157 fmt.write_cfg.aac_cfg.ep_config = cfg->ep_config;
2158 fmt.write_cfg.aac_cfg.section_data_resilience =
2159 cfg->section_data_resilience;
2160 fmt.write_cfg.aac_cfg.scalefactor_data_resilience =
2161 cfg->scalefactor_data_resilience;
2162 fmt.write_cfg.aac_cfg.spectral_data_resilience =
2163 cfg->spectral_data_resilience;
2164 fmt.write_cfg.aac_cfg.ch_cfg = cfg->ch_cfg;
2165 fmt.write_cfg.aac_cfg.sample_rate = cfg->sample_rate;
2166 pr_info("%s:format=%x cfg_size=%d aac-cfg=%x aot=%d ch=%d sr=%d\n",
2167 __func__, fmt.format, fmt.cfg_size,
2168 fmt.write_cfg.aac_cfg.format,
2169 fmt.write_cfg.aac_cfg.aot,
2170 fmt.write_cfg.aac_cfg.ch_cfg,
2171 fmt.write_cfg.aac_cfg.sample_rate);
2172 rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
2173 if (rc < 0) {
2174 pr_err("%s:Comamnd open failed\n", __func__);
2175 goto fail_cmd;
2176 }
2177 rc = wait_event_timeout(ac->cmd_wait,
2178 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2179 if (!rc) {
2180 pr_err("%s:timeout. waited for FORMAT_UPDATE\n", __func__);
2181 goto fail_cmd;
2182 }
2183 return 0;
2184fail_cmd:
2185 return -EINVAL;
2186}
2187
Bharath Ramachandramurthy4f71d502011-10-23 19:45:22 -07002188
2189int q6asm_media_format_block_multi_aac(struct audio_client *ac,
2190 struct asm_aac_cfg *cfg)
2191{
2192 struct asm_stream_media_format_update fmt;
2193 int rc = 0;
2194
2195 pr_debug("%s:session[%d]rate[%d]ch[%d]\n", __func__, ac->session,
2196 cfg->sample_rate, cfg->ch_cfg);
2197
2198 q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
2199
2200 fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FORMAT_UPDATE;
2201
2202 fmt.format = MPEG4_MULTI_AAC;
2203 fmt.cfg_size = sizeof(struct asm_aac_cfg);
2204 fmt.write_cfg.aac_cfg.format = cfg->format;
2205 fmt.write_cfg.aac_cfg.aot = cfg->aot;
2206 fmt.write_cfg.aac_cfg.ep_config = cfg->ep_config;
2207 fmt.write_cfg.aac_cfg.section_data_resilience =
2208 cfg->section_data_resilience;
2209 fmt.write_cfg.aac_cfg.scalefactor_data_resilience =
2210 cfg->scalefactor_data_resilience;
2211 fmt.write_cfg.aac_cfg.spectral_data_resilience =
2212 cfg->spectral_data_resilience;
2213 fmt.write_cfg.aac_cfg.ch_cfg = cfg->ch_cfg;
2214 fmt.write_cfg.aac_cfg.sample_rate = cfg->sample_rate;
2215 pr_info("%s:format=%x cfg_size=%d aac-cfg=%x aot=%d ch=%d sr=%d\n",
2216 __func__, fmt.format, fmt.cfg_size,
2217 fmt.write_cfg.aac_cfg.format,
2218 fmt.write_cfg.aac_cfg.aot,
2219 fmt.write_cfg.aac_cfg.ch_cfg,
2220 fmt.write_cfg.aac_cfg.sample_rate);
2221 rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
2222 if (rc < 0) {
2223 pr_err("%s:Comamnd open failed\n", __func__);
2224 goto fail_cmd;
2225 }
2226 rc = wait_event_timeout(ac->cmd_wait,
2227 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2228 if (!rc) {
2229 pr_err("%s:timeout. waited for FORMAT_UPDATE\n", __func__);
2230 goto fail_cmd;
2231 }
2232 return 0;
2233fail_cmd:
2234 return -EINVAL;
2235}
2236
2237
Alex Wong2caeecc2011-10-28 10:52:15 +05302238
2239int q6asm_media_format_block(struct audio_client *ac, uint32_t format)
2240{
2241
2242 struct asm_stream_media_format_update fmt;
2243 int rc = 0;
2244
2245 pr_debug("%s:session[%d] format[0x%x]\n", __func__,
2246 ac->session, format);
2247
2248 q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
2249 fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FORMAT_UPDATE;
Asish Bhattacharya305d1752011-11-01 20:38:26 +05302250 switch (format) {
2251 case FORMAT_V13K:
2252 fmt.format = V13K_FS;
2253 break;
2254 case FORMAT_EVRC:
2255 fmt.format = EVRC_FS;
2256 break;
2257 case FORMAT_AMRWB:
2258 fmt.format = AMRWB_FS;
2259 break;
2260 case FORMAT_AMRNB:
2261 fmt.format = AMRNB_FS;
2262 break;
2263 case FORMAT_MP3:
2264 fmt.format = MP3;
2265 break;
2266 default:
2267 pr_err("Invalid format[%d]\n", format);
2268 goto fail_cmd;
2269 }
Alex Wong2caeecc2011-10-28 10:52:15 +05302270 fmt.cfg_size = 0;
2271
2272 rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
2273 if (rc < 0) {
2274 pr_err("%s:Comamnd open failed\n", __func__);
2275 goto fail_cmd;
2276 }
2277 rc = wait_event_timeout(ac->cmd_wait,
2278 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2279 if (!rc) {
2280 pr_err("%s:timeout. waited for FORMAT_UPDATE\n", __func__);
2281 goto fail_cmd;
2282 }
2283 return 0;
2284fail_cmd:
2285 return -EINVAL;
2286}
2287
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002288int q6asm_media_format_block_wma(struct audio_client *ac,
2289 void *cfg)
2290{
2291 struct asm_stream_media_format_update fmt;
2292 struct asm_wma_cfg *wma_cfg = (struct asm_wma_cfg *)cfg;
2293 int rc = 0;
2294
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002295 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",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002296 ac->session, wma_cfg->format_tag, wma_cfg->sample_rate,
2297 wma_cfg->ch_cfg, wma_cfg->avg_bytes_per_sec,
2298 wma_cfg->block_align, wma_cfg->valid_bits_per_sample,
2299 wma_cfg->ch_mask, wma_cfg->encode_opt);
2300
2301 q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
2302
2303 fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FORMAT_UPDATE;
2304
2305 fmt.format = WMA_V9;
2306 fmt.cfg_size = sizeof(struct asm_wma_cfg);
2307 fmt.write_cfg.wma_cfg.format_tag = wma_cfg->format_tag;
2308 fmt.write_cfg.wma_cfg.ch_cfg = wma_cfg->ch_cfg;
2309 fmt.write_cfg.wma_cfg.sample_rate = wma_cfg->sample_rate;
2310 fmt.write_cfg.wma_cfg.avg_bytes_per_sec = wma_cfg->avg_bytes_per_sec;
2311 fmt.write_cfg.wma_cfg.block_align = wma_cfg->block_align;
2312 fmt.write_cfg.wma_cfg.valid_bits_per_sample =
2313 wma_cfg->valid_bits_per_sample;
2314 fmt.write_cfg.wma_cfg.ch_mask = wma_cfg->ch_mask;
2315 fmt.write_cfg.wma_cfg.encode_opt = wma_cfg->encode_opt;
2316 fmt.write_cfg.wma_cfg.adv_encode_opt = 0;
2317 fmt.write_cfg.wma_cfg.adv_encode_opt2 = 0;
2318 fmt.write_cfg.wma_cfg.drc_peak_ref = 0;
2319 fmt.write_cfg.wma_cfg.drc_peak_target = 0;
2320 fmt.write_cfg.wma_cfg.drc_ave_ref = 0;
2321 fmt.write_cfg.wma_cfg.drc_ave_target = 0;
2322
2323 rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
2324 if (rc < 0) {
2325 pr_err("%s:Comamnd open failed\n", __func__);
2326 goto fail_cmd;
2327 }
2328 rc = wait_event_timeout(ac->cmd_wait,
2329 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2330 if (!rc) {
2331 pr_err("%s:timeout. waited for FORMAT_UPDATE\n", __func__);
2332 goto fail_cmd;
2333 }
2334 return 0;
2335fail_cmd:
2336 return -EINVAL;
2337}
2338
2339int q6asm_media_format_block_wmapro(struct audio_client *ac,
2340 void *cfg)
2341{
2342 struct asm_stream_media_format_update fmt;
2343 struct asm_wmapro_cfg *wmapro_cfg = (struct asm_wmapro_cfg *)cfg;
2344 int rc = 0;
2345
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002346 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",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002347 ac->session, wmapro_cfg->format_tag, wmapro_cfg->sample_rate,
2348 wmapro_cfg->ch_cfg, wmapro_cfg->avg_bytes_per_sec,
2349 wmapro_cfg->block_align, wmapro_cfg->valid_bits_per_sample,
2350 wmapro_cfg->ch_mask, wmapro_cfg->encode_opt,
2351 wmapro_cfg->adv_encode_opt, wmapro_cfg->adv_encode_opt2);
2352
2353 q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
2354
2355 fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FORMAT_UPDATE;
2356
2357 fmt.format = WMA_V10PRO;
2358 fmt.cfg_size = sizeof(struct asm_wmapro_cfg);
2359 fmt.write_cfg.wmapro_cfg.format_tag = wmapro_cfg->format_tag;
2360 fmt.write_cfg.wmapro_cfg.ch_cfg = wmapro_cfg->ch_cfg;
2361 fmt.write_cfg.wmapro_cfg.sample_rate = wmapro_cfg->sample_rate;
2362 fmt.write_cfg.wmapro_cfg.avg_bytes_per_sec =
2363 wmapro_cfg->avg_bytes_per_sec;
2364 fmt.write_cfg.wmapro_cfg.block_align = wmapro_cfg->block_align;
2365 fmt.write_cfg.wmapro_cfg.valid_bits_per_sample =
2366 wmapro_cfg->valid_bits_per_sample;
2367 fmt.write_cfg.wmapro_cfg.ch_mask = wmapro_cfg->ch_mask;
2368 fmt.write_cfg.wmapro_cfg.encode_opt = wmapro_cfg->encode_opt;
2369 fmt.write_cfg.wmapro_cfg.adv_encode_opt = wmapro_cfg->adv_encode_opt;
2370 fmt.write_cfg.wmapro_cfg.adv_encode_opt2 = wmapro_cfg->adv_encode_opt2;
2371 fmt.write_cfg.wmapro_cfg.drc_peak_ref = 0;
2372 fmt.write_cfg.wmapro_cfg.drc_peak_target = 0;
2373 fmt.write_cfg.wmapro_cfg.drc_ave_ref = 0;
2374 fmt.write_cfg.wmapro_cfg.drc_ave_target = 0;
2375
2376 rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
2377 if (rc < 0) {
2378 pr_err("%s:Comamnd open failed\n", __func__);
2379 goto fail_cmd;
2380 }
2381 rc = wait_event_timeout(ac->cmd_wait,
2382 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2383 if (!rc) {
2384 pr_err("%s:timeout. waited for FORMAT_UPDATE\n", __func__);
2385 goto fail_cmd;
2386 }
2387 return 0;
2388fail_cmd:
2389 return -EINVAL;
2390}
2391
2392int q6asm_memory_map(struct audio_client *ac, uint32_t buf_add, int dir,
2393 uint32_t bufsz, uint32_t bufcnt)
2394{
2395 struct asm_stream_cmd_memory_map mem_map;
2396 int rc = 0;
2397
2398 if (!ac || ac->apr == NULL || this_mmap.apr == NULL) {
2399 pr_err("APR handle NULL\n");
2400 return -EINVAL;
2401 }
2402
2403 pr_debug("%s: Session[%d]\n", __func__, ac->session);
2404
2405 mem_map.hdr.opcode = ASM_SESSION_CMD_MEMORY_MAP;
2406
2407 mem_map.buf_add = buf_add;
2408 mem_map.buf_size = bufsz * bufcnt;
2409 mem_map.mempool_id = 0; /* EBI */
2410 mem_map.reserved = 0;
2411
2412 q6asm_add_mmaphdr(&mem_map.hdr,
2413 sizeof(struct asm_stream_cmd_memory_map), TRUE);
2414
2415 pr_debug("buf add[%x] buf_add_parameter[%x]\n",
2416 mem_map.buf_add, buf_add);
2417
2418 rc = apr_send_pkt(this_mmap.apr, (uint32_t *) &mem_map);
2419 if (rc < 0) {
2420 pr_err("mem_map op[0x%x]rc[%d]\n",
2421 mem_map.hdr.opcode, rc);
2422 rc = -EINVAL;
2423 goto fail_cmd;
2424 }
2425
2426 rc = wait_event_timeout(this_mmap.cmd_wait,
2427 (atomic_read(&this_mmap.cmd_state) == 0), 5 * HZ);
2428 if (!rc) {
2429 pr_err("timeout. waited for memory_map\n");
2430 rc = -EINVAL;
2431 goto fail_cmd;
2432 }
2433 rc = 0;
2434fail_cmd:
2435 return rc;
2436}
2437
2438int q6asm_memory_unmap(struct audio_client *ac, uint32_t buf_add, int dir)
2439{
2440 struct asm_stream_cmd_memory_unmap mem_unmap;
2441 int rc = 0;
2442
2443 if (!ac || ac->apr == NULL || this_mmap.apr == NULL) {
2444 pr_err("APR handle NULL\n");
2445 return -EINVAL;
2446 }
2447 pr_debug("%s: Session[%d]\n", __func__, ac->session);
2448
2449 q6asm_add_mmaphdr(&mem_unmap.hdr,
2450 sizeof(struct asm_stream_cmd_memory_unmap), TRUE);
2451 mem_unmap.hdr.opcode = ASM_SESSION_CMD_MEMORY_UNMAP;
2452 mem_unmap.buf_add = buf_add;
2453
2454 rc = apr_send_pkt(this_mmap.apr, (uint32_t *) &mem_unmap);
2455 if (rc < 0) {
2456 pr_err("mem_unmap op[0x%x]rc[%d]\n",
2457 mem_unmap.hdr.opcode, rc);
2458 rc = -EINVAL;
2459 goto fail_cmd;
2460 }
2461
2462 rc = wait_event_timeout(this_mmap.cmd_wait,
2463 (atomic_read(&this_mmap.cmd_state) == 0), 5 * HZ);
2464 if (!rc) {
2465 pr_err("timeout. waited for memory_map\n");
2466 rc = -EINVAL;
2467 goto fail_cmd;
2468 }
2469 rc = 0;
2470fail_cmd:
2471 return rc;
2472}
2473
2474int q6asm_set_lrgain(struct audio_client *ac, int left_gain, int right_gain)
2475{
2476 void *vol_cmd = NULL;
2477 void *payload = NULL;
2478 struct asm_pp_params_command *cmd = NULL;
2479 struct asm_lrchannel_gain_params *lrgain = NULL;
2480 int sz = 0;
2481 int rc = 0;
2482
2483 sz = sizeof(struct asm_pp_params_command) +
2484 + sizeof(struct asm_lrchannel_gain_params);
2485 vol_cmd = kzalloc(sz, GFP_KERNEL);
2486 if (vol_cmd == NULL) {
2487 pr_err("%s[%d]: Mem alloc failed\n", __func__, ac->session);
2488 rc = -EINVAL;
2489 return rc;
2490 }
2491 cmd = (struct asm_pp_params_command *)vol_cmd;
2492 q6asm_add_hdr_async(ac, &cmd->hdr, sz, TRUE);
2493 cmd->hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS;
2494 cmd->payload = NULL;
2495 cmd->payload_size = sizeof(struct asm_pp_param_data_hdr) +
2496 sizeof(struct asm_lrchannel_gain_params);
2497 cmd->params.module_id = VOLUME_CONTROL_MODULE_ID;
2498 cmd->params.param_id = L_R_CHANNEL_GAIN_PARAM_ID;
2499 cmd->params.param_size = sizeof(struct asm_lrchannel_gain_params);
2500 cmd->params.reserved = 0;
2501
2502 payload = (u8 *)(vol_cmd + sizeof(struct asm_pp_params_command));
2503 lrgain = (struct asm_lrchannel_gain_params *)payload;
2504
2505 lrgain->left_gain = left_gain;
2506 lrgain->right_gain = right_gain;
2507 rc = apr_send_pkt(ac->apr, (uint32_t *) vol_cmd);
2508 if (rc < 0) {
2509 pr_err("%s: Volume Command failed\n", __func__);
2510 rc = -EINVAL;
2511 goto fail_cmd;
2512 }
2513
2514 rc = wait_event_timeout(ac->cmd_wait,
2515 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2516 if (!rc) {
2517 pr_err("%s: timeout in sending volume command to apr\n",
2518 __func__);
2519 rc = -EINVAL;
2520 goto fail_cmd;
2521 }
2522 rc = 0;
2523fail_cmd:
2524 kfree(vol_cmd);
2525 return rc;
2526}
2527
2528static int q6asm_memory_map_regions(struct audio_client *ac, int dir,
2529 uint32_t bufsz, uint32_t bufcnt)
2530{
2531 struct asm_stream_cmd_memory_map_regions *mmap_regions = NULL;
2532 struct asm_memory_map_regions *mregions = NULL;
2533 struct audio_port_data *port = NULL;
2534 struct audio_buffer *ab = NULL;
2535 void *mmap_region_cmd = NULL;
2536 void *payload = NULL;
2537 int rc = 0;
2538 int i = 0;
2539 int cmd_size = 0;
2540
2541 if (!ac || ac->apr == NULL || this_mmap.apr == NULL) {
2542 pr_err("APR handle NULL\n");
2543 return -EINVAL;
2544 }
2545 pr_debug("%s: Session[%d]\n", __func__, ac->session);
2546
2547 cmd_size = sizeof(struct asm_stream_cmd_memory_map_regions)
2548 + sizeof(struct asm_memory_map_regions) * bufcnt;
2549
2550 mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
Vasudeva Rao Thumati86edf6c2011-07-06 16:25:13 +05302551 if (mmap_region_cmd == NULL) {
2552 pr_err("%s: Mem alloc failed\n", __func__);
2553 rc = -EINVAL;
2554 return rc;
2555 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002556 mmap_regions = (struct asm_stream_cmd_memory_map_regions *)
2557 mmap_region_cmd;
2558 q6asm_add_mmaphdr(&mmap_regions->hdr, cmd_size, TRUE);
2559 mmap_regions->hdr.opcode = ASM_SESSION_CMD_MEMORY_MAP_REGIONS;
2560 mmap_regions->mempool_id = 0;
2561 mmap_regions->nregions = bufcnt & 0x00ff;
2562 pr_debug("map_regions->nregions = %d\n", mmap_regions->nregions);
2563 payload = ((u8 *) mmap_region_cmd +
2564 sizeof(struct asm_stream_cmd_memory_map_regions));
2565 mregions = (struct asm_memory_map_regions *)payload;
2566
2567 port = &ac->port[dir];
2568 for (i = 0; i < bufcnt; i++) {
2569 ab = &port->buf[i];
2570 mregions->phys = ab->phys;
2571 mregions->buf_size = ab->size;
2572 ++mregions;
2573 }
2574
2575 rc = apr_send_pkt(this_mmap.apr, (uint32_t *) mmap_region_cmd);
2576 if (rc < 0) {
2577 pr_err("mmap_regions op[0x%x]rc[%d]\n",
2578 mmap_regions->hdr.opcode, rc);
2579 rc = -EINVAL;
2580 goto fail_cmd;
2581 }
2582
2583 rc = wait_event_timeout(this_mmap.cmd_wait,
2584 (atomic_read(&this_mmap.cmd_state) == 0), 5*HZ);
2585 if (!rc) {
2586 pr_err("timeout. waited for memory_map\n");
2587 rc = -EINVAL;
2588 goto fail_cmd;
2589 }
2590 rc = 0;
2591fail_cmd:
2592 kfree(mmap_region_cmd);
2593 return rc;
2594}
2595
2596static int q6asm_memory_unmap_regions(struct audio_client *ac, int dir,
2597 uint32_t bufsz, uint32_t bufcnt)
2598{
2599 struct asm_stream_cmd_memory_unmap_regions *unmap_regions = NULL;
2600 struct asm_memory_unmap_regions *mregions = NULL;
2601 struct audio_port_data *port = NULL;
2602 struct audio_buffer *ab = NULL;
2603 void *unmap_region_cmd = NULL;
2604 void *payload = NULL;
2605 int rc = 0;
2606 int i = 0;
2607 int cmd_size = 0;
2608
2609 if (!ac || ac->apr == NULL || this_mmap.apr == NULL) {
2610 pr_err("APR handle NULL\n");
2611 return -EINVAL;
2612 }
2613 pr_debug("%s: Session[%d]\n", __func__, ac->session);
2614
2615 cmd_size = sizeof(struct asm_stream_cmd_memory_unmap_regions) +
2616 sizeof(struct asm_memory_unmap_regions) * bufcnt;
2617
2618 unmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
Vasudeva Rao Thumati86edf6c2011-07-06 16:25:13 +05302619 if (unmap_region_cmd == NULL) {
2620 pr_err("%s: Mem alloc failed\n", __func__);
2621 rc = -EINVAL;
2622 return rc;
2623 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002624 unmap_regions = (struct asm_stream_cmd_memory_unmap_regions *)
2625 unmap_region_cmd;
2626 q6asm_add_mmaphdr(&unmap_regions->hdr, cmd_size, TRUE);
2627 unmap_regions->hdr.opcode = ASM_SESSION_CMD_MEMORY_UNMAP_REGIONS;
2628 unmap_regions->nregions = bufcnt & 0x00ff;
2629 pr_debug("unmap_regions->nregions = %d\n", unmap_regions->nregions);
2630 payload = ((u8 *) unmap_region_cmd +
2631 sizeof(struct asm_stream_cmd_memory_unmap_regions));
2632 mregions = (struct asm_memory_unmap_regions *)payload;
2633 port = &ac->port[dir];
2634 for (i = 0; i < bufcnt; i++) {
2635 ab = &port->buf[i];
2636 mregions->phys = ab->phys;
2637 ++mregions;
2638 }
2639
2640 rc = apr_send_pkt(this_mmap.apr, (uint32_t *) unmap_region_cmd);
2641 if (rc < 0) {
2642 pr_err("mmap_regions op[0x%x]rc[%d]\n",
2643 unmap_regions->hdr.opcode, rc);
2644 goto fail_cmd;
2645 }
2646
2647 rc = wait_event_timeout(this_mmap.cmd_wait,
2648 (atomic_read(&this_mmap.cmd_state) == 0), 5*HZ);
2649 if (!rc) {
2650 pr_err("timeout. waited for memory_unmap\n");
2651 goto fail_cmd;
2652 }
2653 rc = 0;
2654
2655fail_cmd:
2656 kfree(unmap_region_cmd);
2657 return rc;
2658}
2659
2660int q6asm_set_mute(struct audio_client *ac, int muteflag)
2661{
2662 void *vol_cmd = NULL;
2663 void *payload = NULL;
2664 struct asm_pp_params_command *cmd = NULL;
2665 struct asm_mute_params *mute = NULL;
2666 int sz = 0;
2667 int rc = 0;
2668
2669 sz = sizeof(struct asm_pp_params_command) +
2670 + sizeof(struct asm_mute_params);
2671 vol_cmd = kzalloc(sz, GFP_KERNEL);
2672 if (vol_cmd == NULL) {
2673 pr_err("%s[%d]: Mem alloc failed\n", __func__, ac->session);
2674 rc = -EINVAL;
2675 return rc;
2676 }
2677 cmd = (struct asm_pp_params_command *)vol_cmd;
2678 q6asm_add_hdr_async(ac, &cmd->hdr, sz, TRUE);
2679 cmd->hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS;
2680 cmd->payload = NULL;
2681 cmd->payload_size = sizeof(struct asm_pp_param_data_hdr) +
2682 sizeof(struct asm_mute_params);
2683 cmd->params.module_id = VOLUME_CONTROL_MODULE_ID;
2684 cmd->params.param_id = MUTE_CONFIG_PARAM_ID;
2685 cmd->params.param_size = sizeof(struct asm_mute_params);
2686 cmd->params.reserved = 0;
2687
2688 payload = (u8 *)(vol_cmd + sizeof(struct asm_pp_params_command));
2689 mute = (struct asm_mute_params *)payload;
2690
2691 mute->muteflag = muteflag;
2692 rc = apr_send_pkt(ac->apr, (uint32_t *) vol_cmd);
2693 if (rc < 0) {
2694 pr_err("%s: Mute Command failed\n", __func__);
2695 rc = -EINVAL;
2696 goto fail_cmd;
2697 }
2698
2699 rc = wait_event_timeout(ac->cmd_wait,
2700 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2701 if (!rc) {
2702 pr_err("%s: timeout in sending mute command to apr\n",
2703 __func__);
2704 rc = -EINVAL;
2705 goto fail_cmd;
2706 }
2707 rc = 0;
2708fail_cmd:
2709 kfree(vol_cmd);
2710 return rc;
2711}
2712
2713int q6asm_set_volume(struct audio_client *ac, int volume)
2714{
2715 void *vol_cmd = NULL;
2716 void *payload = NULL;
2717 struct asm_pp_params_command *cmd = NULL;
2718 struct asm_master_gain_params *mgain = NULL;
2719 int sz = 0;
2720 int rc = 0;
2721
2722 sz = sizeof(struct asm_pp_params_command) +
2723 + sizeof(struct asm_master_gain_params);
2724 vol_cmd = kzalloc(sz, GFP_KERNEL);
2725 if (vol_cmd == NULL) {
2726 pr_err("%s[%d]: Mem alloc failed\n", __func__, ac->session);
2727 rc = -EINVAL;
2728 return rc;
2729 }
2730 cmd = (struct asm_pp_params_command *)vol_cmd;
2731 q6asm_add_hdr_async(ac, &cmd->hdr, sz, TRUE);
2732 cmd->hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS;
2733 cmd->payload = NULL;
2734 cmd->payload_size = sizeof(struct asm_pp_param_data_hdr) +
2735 sizeof(struct asm_master_gain_params);
2736 cmd->params.module_id = VOLUME_CONTROL_MODULE_ID;
2737 cmd->params.param_id = MASTER_GAIN_PARAM_ID;
2738 cmd->params.param_size = sizeof(struct asm_master_gain_params);
2739 cmd->params.reserved = 0;
2740
2741 payload = (u8 *)(vol_cmd + sizeof(struct asm_pp_params_command));
2742 mgain = (struct asm_master_gain_params *)payload;
2743
2744 mgain->master_gain = volume;
2745 mgain->padding = 0x00;
2746 rc = apr_send_pkt(ac->apr, (uint32_t *) vol_cmd);
2747 if (rc < 0) {
2748 pr_err("%s: Volume Command failed\n", __func__);
2749 rc = -EINVAL;
2750 goto fail_cmd;
2751 }
2752
2753 rc = wait_event_timeout(ac->cmd_wait,
2754 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2755 if (!rc) {
2756 pr_err("%s: timeout in sending volume command to apr\n",
2757 __func__);
2758 rc = -EINVAL;
2759 goto fail_cmd;
2760 }
2761 rc = 0;
2762fail_cmd:
2763 kfree(vol_cmd);
2764 return rc;
2765}
2766
2767int q6asm_set_softpause(struct audio_client *ac,
2768 struct asm_softpause_params *pause_param)
2769{
2770 void *vol_cmd = NULL;
2771 void *payload = NULL;
2772 struct asm_pp_params_command *cmd = NULL;
2773 struct asm_softpause_params *params = NULL;
2774 int sz = 0;
2775 int rc = 0;
2776
2777 sz = sizeof(struct asm_pp_params_command) +
2778 + sizeof(struct asm_softpause_params);
2779 vol_cmd = kzalloc(sz, GFP_KERNEL);
2780 if (vol_cmd == NULL) {
2781 pr_err("%s[%d]: Mem alloc failed\n", __func__, ac->session);
2782 rc = -EINVAL;
2783 return rc;
2784 }
2785 cmd = (struct asm_pp_params_command *)vol_cmd;
2786 q6asm_add_hdr_async(ac, &cmd->hdr, sz, TRUE);
2787 cmd->hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS;
2788 cmd->payload = NULL;
2789 cmd->payload_size = sizeof(struct asm_pp_param_data_hdr) +
2790 sizeof(struct asm_softpause_params);
2791 cmd->params.module_id = VOLUME_CONTROL_MODULE_ID;
2792 cmd->params.param_id = SOFT_PAUSE_PARAM_ID;
2793 cmd->params.param_size = sizeof(struct asm_softpause_params);
2794 cmd->params.reserved = 0;
2795
2796 payload = (u8 *)(vol_cmd + sizeof(struct asm_pp_params_command));
2797 params = (struct asm_softpause_params *)payload;
2798
2799 params->enable = pause_param->enable;
2800 params->period = pause_param->period;
2801 params->step = pause_param->step;
2802 params->rampingcurve = pause_param->rampingcurve;
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002803 pr_debug("%s: soft Pause Command: enable = %d, period = %d, step = %d, curve = %d\n",
2804 __func__, params->enable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002805 params->period, params->step, params->rampingcurve);
2806 rc = apr_send_pkt(ac->apr, (uint32_t *) vol_cmd);
2807 if (rc < 0) {
2808 pr_err("%s: Volume Command(soft_pause) failed\n", __func__);
2809 rc = -EINVAL;
2810 goto fail_cmd;
2811 }
2812
2813 rc = wait_event_timeout(ac->cmd_wait,
2814 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2815 if (!rc) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002816 pr_err("%s: timeout in sending volume command(soft_pause) to apr\n",
2817 __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002818 rc = -EINVAL;
2819 goto fail_cmd;
2820 }
2821 rc = 0;
2822fail_cmd:
2823 kfree(vol_cmd);
2824 return rc;
2825}
2826
Swaminathan Sathappanb0021cd2011-08-31 15:20:12 -07002827int q6asm_set_softvolume(struct audio_client *ac,
2828 struct asm_softvolume_params *softvol_param)
2829{
2830 void *vol_cmd = NULL;
2831 void *payload = NULL;
2832 struct asm_pp_params_command *cmd = NULL;
2833 struct asm_softvolume_params *params = NULL;
2834 int sz = 0;
2835 int rc = 0;
2836
2837 sz = sizeof(struct asm_pp_params_command) +
2838 + sizeof(struct asm_softvolume_params);
2839 vol_cmd = kzalloc(sz, GFP_KERNEL);
2840 if (vol_cmd == NULL) {
2841 pr_err("%s[%d]: Mem alloc failed\n", __func__, ac->session);
2842 rc = -EINVAL;
2843 return rc;
2844 }
2845 cmd = (struct asm_pp_params_command *)vol_cmd;
2846 q6asm_add_hdr_async(ac, &cmd->hdr, sz, TRUE);
2847 cmd->hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS;
2848 cmd->payload = NULL;
2849 cmd->payload_size = sizeof(struct asm_pp_param_data_hdr) +
2850 sizeof(struct asm_softvolume_params);
2851 cmd->params.module_id = VOLUME_CONTROL_MODULE_ID;
2852 cmd->params.param_id = SOFT_VOLUME_PARAM_ID;
2853 cmd->params.param_size = sizeof(struct asm_softvolume_params);
2854 cmd->params.reserved = 0;
2855
2856 payload = (u8 *)(vol_cmd + sizeof(struct asm_pp_params_command));
2857 params = (struct asm_softvolume_params *)payload;
2858
2859 params->period = softvol_param->period;
2860 params->step = softvol_param->step;
2861 params->rampingcurve = softvol_param->rampingcurve;
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002862 pr_debug("%s: soft Volume:opcode = %d,payload_sz =%d,module_id =%d, param_id = %d, param_sz = %d\n",
2863 __func__,
Swaminathan Sathappanb0021cd2011-08-31 15:20:12 -07002864 cmd->hdr.opcode, cmd->payload_size,
2865 cmd->params.module_id, cmd->params.param_id,
2866 cmd->params.param_size);
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002867 pr_debug("%s: soft Volume Command: period = %d, step = %d, curve = %d\n",
2868 __func__, params->period,
Swaminathan Sathappanb0021cd2011-08-31 15:20:12 -07002869 params->step, params->rampingcurve);
2870 rc = apr_send_pkt(ac->apr, (uint32_t *) vol_cmd);
2871 if (rc < 0) {
2872 pr_err("%s: Volume Command(soft_volume) failed\n", __func__);
2873 rc = -EINVAL;
2874 goto fail_cmd;
2875 }
2876
2877 rc = wait_event_timeout(ac->cmd_wait,
2878 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2879 if (!rc) {
Harmandeep Singheaf59b42012-06-05 21:46:02 -07002880 pr_err("%s: timeout in sending volume command(soft_volume) to apr\n",
2881 __func__);
Swaminathan Sathappanb0021cd2011-08-31 15:20:12 -07002882 rc = -EINVAL;
2883 goto fail_cmd;
2884 }
2885 rc = 0;
2886fail_cmd:
2887 kfree(vol_cmd);
2888 return rc;
2889}
2890
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002891int q6asm_equalizer(struct audio_client *ac, void *eq)
2892{
2893 void *eq_cmd = NULL;
2894 void *payload = NULL;
2895 struct asm_pp_params_command *cmd = NULL;
2896 struct asm_equalizer_params *equalizer = NULL;
2897 struct msm_audio_eq_stream_config *eq_params = NULL;
2898 int i = 0;
2899 int sz = 0;
2900 int rc = 0;
2901
2902 sz = sizeof(struct asm_pp_params_command) +
2903 + sizeof(struct asm_equalizer_params);
2904 eq_cmd = kzalloc(sz, GFP_KERNEL);
2905 if (eq_cmd == NULL) {
2906 pr_err("%s[%d]: Mem alloc failed\n", __func__, ac->session);
2907 rc = -EINVAL;
2908 goto fail_cmd;
2909 }
2910 eq_params = (struct msm_audio_eq_stream_config *) eq;
2911 cmd = (struct asm_pp_params_command *)eq_cmd;
2912 q6asm_add_hdr(ac, &cmd->hdr, sz, TRUE);
2913 cmd->hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS;
2914 cmd->payload = NULL;
2915 cmd->payload_size = sizeof(struct asm_pp_param_data_hdr) +
2916 sizeof(struct asm_equalizer_params);
2917 cmd->params.module_id = EQUALIZER_MODULE_ID;
2918 cmd->params.param_id = EQUALIZER_PARAM_ID;
2919 cmd->params.param_size = sizeof(struct asm_equalizer_params);
2920 cmd->params.reserved = 0;
2921 payload = (u8 *)(eq_cmd + sizeof(struct asm_pp_params_command));
2922 equalizer = (struct asm_equalizer_params *)payload;
2923
2924 equalizer->enable = eq_params->enable;
2925 equalizer->num_bands = eq_params->num_bands;
2926 pr_debug("%s: enable:%d numbands:%d\n", __func__, eq_params->enable,
2927 eq_params->num_bands);
2928 for (i = 0; i < eq_params->num_bands; i++) {
2929 equalizer->eq_bands[i].band_idx =
2930 eq_params->eq_bands[i].band_idx;
2931 equalizer->eq_bands[i].filter_type =
2932 eq_params->eq_bands[i].filter_type;
2933 equalizer->eq_bands[i].center_freq_hz =
2934 eq_params->eq_bands[i].center_freq_hz;
2935 equalizer->eq_bands[i].filter_gain =
2936 eq_params->eq_bands[i].filter_gain;
2937 equalizer->eq_bands[i].q_factor =
2938 eq_params->eq_bands[i].q_factor;
2939 pr_debug("%s: filter_type:%u bandnum:%d\n", __func__,
2940 eq_params->eq_bands[i].filter_type, i);
2941 pr_debug("%s: center_freq_hz:%u bandnum:%d\n", __func__,
2942 eq_params->eq_bands[i].center_freq_hz, i);
2943 pr_debug("%s: filter_gain:%d bandnum:%d\n", __func__,
2944 eq_params->eq_bands[i].filter_gain, i);
2945 pr_debug("%s: q_factor:%d bandnum:%d\n", __func__,
2946 eq_params->eq_bands[i].q_factor, i);
2947 }
2948 rc = apr_send_pkt(ac->apr, (uint32_t *) eq_cmd);
2949 if (rc < 0) {
2950 pr_err("%s: Equalizer Command failed\n", __func__);
2951 rc = -EINVAL;
2952 goto fail_cmd;
2953 }
2954
2955 rc = wait_event_timeout(ac->cmd_wait,
2956 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
2957 if (!rc) {
2958 pr_err("%s: timeout in sending equalizer command to apr\n",
2959 __func__);
2960 rc = -EINVAL;
2961 goto fail_cmd;
2962 }
2963 rc = 0;
2964fail_cmd:
2965 kfree(eq_cmd);
2966 return rc;
2967}
2968
2969int q6asm_read(struct audio_client *ac)
2970{
2971 struct asm_stream_cmd_read read;
2972 struct audio_buffer *ab;
2973 int dsp_buf;
2974 struct audio_port_data *port;
2975 int rc;
2976 if (!ac || ac->apr == NULL) {
2977 pr_err("APR handle NULL\n");
2978 return -EINVAL;
2979 }
2980 if (ac->io_mode == SYNC_IO_MODE) {
2981 port = &ac->port[OUT];
2982
2983 q6asm_add_hdr(ac, &read.hdr, sizeof(read), FALSE);
2984
2985 mutex_lock(&port->lock);
2986
2987 dsp_buf = port->dsp_buf;
2988 ab = &port->buf[dsp_buf];
2989
2990 pr_debug("%s:session[%d]dsp-buf[%d][%p]cpu_buf[%d][%p]\n",
2991 __func__,
2992 ac->session,
2993 dsp_buf,
2994 (void *)port->buf[dsp_buf].data,
2995 port->cpu_buf,
2996 (void *)port->buf[port->cpu_buf].phys);
2997
2998 read.hdr.opcode = ASM_DATA_CMD_READ;
2999 read.buf_add = ab->phys;
3000 read.buf_size = ab->size;
3001 read.uid = port->dsp_buf;
3002 read.hdr.token = port->dsp_buf;
3003
3004 port->dsp_buf = (port->dsp_buf + 1) & (port->max_buf_cnt - 1);
3005 mutex_unlock(&port->lock);
3006 pr_debug("%s:buf add[0x%x] token[%d] uid[%d]\n", __func__,
3007 read.buf_add,
3008 read.hdr.token,
3009 read.uid);
3010 rc = apr_send_pkt(ac->apr, (uint32_t *) &read);
3011 if (rc < 0) {
3012 pr_err("read op[0x%x]rc[%d]\n", read.hdr.opcode, rc);
3013 goto fail_cmd;
3014 }
3015 return 0;
3016 }
3017fail_cmd:
3018 return -EINVAL;
3019}
3020
3021int q6asm_read_nolock(struct audio_client *ac)
3022{
3023 struct asm_stream_cmd_read read;
3024 struct audio_buffer *ab;
3025 int dsp_buf;
3026 struct audio_port_data *port;
3027 int rc;
3028 if (!ac || ac->apr == NULL) {
3029 pr_err("APR handle NULL\n");
3030 return -EINVAL;
3031 }
3032 if (ac->io_mode == SYNC_IO_MODE) {
3033 port = &ac->port[OUT];
3034
3035 q6asm_add_hdr_async(ac, &read.hdr, sizeof(read), FALSE);
3036
3037
3038 dsp_buf = port->dsp_buf;
3039 ab = &port->buf[dsp_buf];
3040
3041 pr_debug("%s:session[%d]dsp-buf[%d][%p]cpu_buf[%d][%p]\n",
3042 __func__,
3043 ac->session,
3044 dsp_buf,
3045 (void *)port->buf[dsp_buf].data,
3046 port->cpu_buf,
3047 (void *)port->buf[port->cpu_buf].phys);
3048
3049 read.hdr.opcode = ASM_DATA_CMD_READ;
3050 read.buf_add = ab->phys;
3051 read.buf_size = ab->size;
3052 read.uid = port->dsp_buf;
3053 read.hdr.token = port->dsp_buf;
3054
3055 port->dsp_buf = (port->dsp_buf + 1) & (port->max_buf_cnt - 1);
3056 pr_debug("%s:buf add[0x%x] token[%d] uid[%d]\n", __func__,
3057 read.buf_add,
3058 read.hdr.token,
3059 read.uid);
3060 rc = apr_send_pkt(ac->apr, (uint32_t *) &read);
3061 if (rc < 0) {
3062 pr_err("read op[0x%x]rc[%d]\n", read.hdr.opcode, rc);
3063 goto fail_cmd;
3064 }
3065 return 0;
3066 }
3067fail_cmd:
3068 return -EINVAL;
3069}
3070
3071
3072static void q6asm_add_hdr_async(struct audio_client *ac, struct apr_hdr *hdr,
3073 uint32_t pkt_size, uint32_t cmd_flg)
3074{
3075 pr_debug("session=%d pkt size=%d cmd_flg=%d\n", pkt_size, cmd_flg,
3076 ac->session);
3077 hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, \
3078 APR_HDR_LEN(sizeof(struct apr_hdr)),\
3079 APR_PKT_VER);
3080 hdr->src_svc = ((struct apr_svc *)ac->apr)->id;
3081 hdr->src_domain = APR_DOMAIN_APPS;
3082 hdr->dest_svc = APR_SVC_ASM;
3083 hdr->dest_domain = APR_DOMAIN_ADSP;
3084 hdr->src_port = ((ac->session << 8) & 0xFF00) | 0x01;
3085 hdr->dest_port = ((ac->session << 8) & 0xFF00) | 0x01;
3086 if (cmd_flg) {
3087 hdr->token = ac->session;
3088 atomic_set(&ac->cmd_state, 1);
3089 }
3090 hdr->pkt_size = pkt_size;
3091 return;
3092}
3093
3094int q6asm_async_write(struct audio_client *ac,
3095 struct audio_aio_write_param *param)
3096{
3097 int rc = 0;
3098 struct asm_stream_cmd_write write;
3099
3100 if (!ac || ac->apr == NULL) {
3101 pr_err("%s: APR handle NULL\n", __func__);
3102 return -EINVAL;
3103 }
3104
3105 q6asm_add_hdr_async(ac, &write.hdr, sizeof(write), FALSE);
3106
3107 /* Pass physical address as token for AIO scheme */
3108 write.hdr.token = param->uid;
3109 write.hdr.opcode = ASM_DATA_CMD_WRITE;
3110 write.buf_add = param->paddr;
3111 write.avail_bytes = param->len;
3112 write.uid = param->uid;
3113 write.msw_ts = param->msw_ts;
3114 write.lsw_ts = param->lsw_ts;
3115 /* Use 0xFF00 for disabling timestamps */
3116 if (param->flags == 0xFF00)
3117 write.uflags = (0x00000000 | (param->flags & 0x800000FF));
3118 else
3119 write.uflags = (0x80000000 | param->flags);
3120
3121 pr_debug("%s: session[%d] bufadd[0x%x]len[0x%x]", __func__, ac->session,
3122 write.buf_add, write.avail_bytes);
3123
3124 rc = apr_send_pkt(ac->apr, (uint32_t *) &write);
3125 if (rc < 0) {
3126 pr_debug("[%s] write op[0x%x]rc[%d]\n", __func__,
3127 write.hdr.opcode, rc);
3128 goto fail_cmd;
3129 }
3130 return 0;
3131fail_cmd:
3132 return -EINVAL;
3133}
3134
3135int q6asm_async_read(struct audio_client *ac,
3136 struct audio_aio_read_param *param)
3137{
3138 int rc = 0;
3139 struct asm_stream_cmd_read read;
3140
3141 if (!ac || ac->apr == NULL) {
3142 pr_err("%s: APR handle NULL\n", __func__);
3143 return -EINVAL;
3144 }
3145
3146 q6asm_add_hdr_async(ac, &read.hdr, sizeof(read), FALSE);
3147
3148 /* Pass physical address as token for AIO scheme */
3149 read.hdr.token = param->paddr;
3150 read.hdr.opcode = ASM_DATA_CMD_READ;
3151 read.buf_add = param->paddr;
3152 read.buf_size = param->len;
3153 read.uid = param->uid;
3154
3155 pr_debug("%s: session[%d] bufadd[0x%x]len[0x%x]", __func__, ac->session,
3156 read.buf_add, read.buf_size);
3157
3158 rc = apr_send_pkt(ac->apr, (uint32_t *) &read);
3159 if (rc < 0) {
3160 pr_debug("[%s] read op[0x%x]rc[%d]\n", __func__,
3161 read.hdr.opcode, rc);
3162 goto fail_cmd;
3163 }
3164 return 0;
3165fail_cmd:
3166 return -EINVAL;
3167}
3168
3169int q6asm_write(struct audio_client *ac, uint32_t len, uint32_t msw_ts,
3170 uint32_t lsw_ts, uint32_t flags)
3171{
3172 int rc = 0;
3173 struct asm_stream_cmd_write write;
3174 struct audio_port_data *port;
3175 struct audio_buffer *ab;
3176 int dsp_buf = 0;
3177
3178 if (!ac || ac->apr == NULL) {
3179 pr_err("APR handle NULL\n");
3180 return -EINVAL;
3181 }
3182 pr_debug("%s: session[%d] len=%d", __func__, ac->session, len);
3183 if (ac->io_mode == SYNC_IO_MODE) {
3184 port = &ac->port[IN];
3185
3186 q6asm_add_hdr(ac, &write.hdr, sizeof(write),
3187 FALSE);
3188 mutex_lock(&port->lock);
3189
3190 dsp_buf = port->dsp_buf;
3191 ab = &port->buf[dsp_buf];
3192
3193 write.hdr.token = port->dsp_buf;
3194 write.hdr.opcode = ASM_DATA_CMD_WRITE;
3195 write.buf_add = ab->phys;
3196 write.avail_bytes = len;
3197 write.uid = port->dsp_buf;
3198 write.msw_ts = msw_ts;
3199 write.lsw_ts = lsw_ts;
3200 /* Use 0xFF00 for disabling timestamps */
3201 if (flags == 0xFF00)
3202 write.uflags = (0x00000000 | (flags & 0x800000FF));
3203 else
3204 write.uflags = (0x80000000 | flags);
3205 port->dsp_buf = (port->dsp_buf + 1) & (port->max_buf_cnt - 1);
3206
3207 pr_debug("%s:ab->phys[0x%x]bufadd[0x%x]token[0x%x]buf_id[0x%x]"
3208 , __func__,
3209 ab->phys,
3210 write.buf_add,
3211 write.hdr.token,
3212 write.uid);
3213 mutex_unlock(&port->lock);
Rajesha Kini3498c932011-07-19 19:58:27 +05303214#ifdef CONFIG_DEBUG_FS
3215 if (out_enable_flag) {
3216 char zero_pattern[2] = {0x00, 0x00};
3217 /* If First two byte is non zero and last two byte
3218 is zero then it is warm output pattern */
3219 if ((strncmp(((char *)ab->data), zero_pattern, 2)) &&
3220 (!strncmp(((char *)ab->data + 2), zero_pattern, 2))) {
3221 do_gettimeofday(&out_warm_tv);
Harmandeep Singheaf59b42012-06-05 21:46:02 -07003222 pr_debug("WARM:apr_send_pkt at %ld sec %ld microsec\n",
3223 out_warm_tv.tv_sec,\
Rajesha Kini3498c932011-07-19 19:58:27 +05303224 out_warm_tv.tv_usec);
3225 pr_debug("Warm Pattern Matched");
3226 }
3227 /* If First two byte is zero and last two byte is
3228 non zero then it is cont ouput pattern */
3229 else if ((!strncmp(((char *)ab->data), zero_pattern, 2))
3230 && (strncmp(((char *)ab->data + 2), zero_pattern, 2))) {
3231 do_gettimeofday(&out_cont_tv);
Harmandeep Singheaf59b42012-06-05 21:46:02 -07003232 pr_debug("CONT:apr_send_pkt at %ld sec %ld microsec\n",
3233 out_cont_tv.tv_sec,\
Rajesha Kini3498c932011-07-19 19:58:27 +05303234 out_cont_tv.tv_usec);
3235 pr_debug("Cont Pattern Matched");
3236 }
3237 }
3238#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003239 rc = apr_send_pkt(ac->apr, (uint32_t *) &write);
3240 if (rc < 0) {
3241 pr_err("write op[0x%x]rc[%d]\n", write.hdr.opcode, rc);
3242 goto fail_cmd;
3243 }
3244 pr_debug("%s: WRITE SUCCESS\n", __func__);
3245 return 0;
3246 }
3247fail_cmd:
3248 return -EINVAL;
3249}
3250
3251int q6asm_write_nolock(struct audio_client *ac, uint32_t len, uint32_t msw_ts,
3252 uint32_t lsw_ts, uint32_t flags)
3253{
3254 int rc = 0;
3255 struct asm_stream_cmd_write write;
3256 struct audio_port_data *port;
3257 struct audio_buffer *ab;
3258 int dsp_buf = 0;
3259
3260 if (!ac || ac->apr == NULL) {
3261 pr_err("APR handle NULL\n");
3262 return -EINVAL;
3263 }
3264 pr_debug("%s: session[%d] len=%d", __func__, ac->session, len);
3265 if (ac->io_mode == SYNC_IO_MODE) {
3266 port = &ac->port[IN];
3267
3268 q6asm_add_hdr_async(ac, &write.hdr, sizeof(write),
3269 FALSE);
3270
3271 dsp_buf = port->dsp_buf;
3272 ab = &port->buf[dsp_buf];
3273
3274 write.hdr.token = port->dsp_buf;
3275 write.hdr.opcode = ASM_DATA_CMD_WRITE;
3276 write.buf_add = ab->phys;
3277 write.avail_bytes = len;
3278 write.uid = port->dsp_buf;
3279 write.msw_ts = msw_ts;
3280 write.lsw_ts = lsw_ts;
3281 /* Use 0xFF00 for disabling timestamps */
3282 if (flags == 0xFF00)
3283 write.uflags = (0x00000000 | (flags & 0x800000FF));
3284 else
3285 write.uflags = (0x80000000 | flags);
3286 port->dsp_buf = (port->dsp_buf + 1) & (port->max_buf_cnt - 1);
3287
3288 pr_debug("%s:ab->phys[0x%x]bufadd[0x%x]token[0x%x]buf_id[0x%x]"
3289 , __func__,
3290 ab->phys,
3291 write.buf_add,
3292 write.hdr.token,
3293 write.uid);
3294
3295 rc = apr_send_pkt(ac->apr, (uint32_t *) &write);
3296 if (rc < 0) {
3297 pr_err("write op[0x%x]rc[%d]\n", write.hdr.opcode, rc);
3298 goto fail_cmd;
3299 }
3300 pr_debug("%s: WRITE SUCCESS\n", __func__);
3301 return 0;
3302 }
3303fail_cmd:
3304 return -EINVAL;
3305}
3306
3307uint64_t q6asm_get_session_time(struct audio_client *ac)
3308{
3309 struct apr_hdr hdr;
3310 int rc;
3311
3312 if (!ac || ac->apr == NULL) {
3313 pr_err("APR handle NULL\n");
3314 return -EINVAL;
3315 }
3316 q6asm_add_hdr(ac, &hdr, sizeof(hdr), TRUE);
3317 hdr.opcode = ASM_SESSION_CMD_GET_SESSION_TIME;
3318 atomic_set(&ac->time_flag, 1);
3319
3320 pr_debug("%s: session[%d]opcode[0x%x]\n", __func__,
3321 ac->session,
3322 hdr.opcode);
3323 rc = apr_send_pkt(ac->apr, (uint32_t *) &hdr);
3324 if (rc < 0) {
3325 pr_err("Commmand 0x%x failed\n", hdr.opcode);
3326 goto fail_cmd;
3327 }
3328 rc = wait_event_timeout(ac->time_wait,
3329 (atomic_read(&ac->time_flag) == 0), 5*HZ);
3330 if (!rc) {
3331 pr_err("%s: timeout in getting session time from DSP\n",
3332 __func__);
3333 goto fail_cmd;
3334 }
3335 return ac->time_stamp;
3336
3337fail_cmd:
3338 return -EINVAL;
3339}
3340
3341int q6asm_cmd(struct audio_client *ac, int cmd)
3342{
3343 struct apr_hdr hdr;
3344 int rc;
3345 atomic_t *state;
3346 int cnt = 0;
3347
3348 if (!ac || ac->apr == NULL) {
3349 pr_err("APR handle NULL\n");
3350 return -EINVAL;
3351 }
3352 q6asm_add_hdr(ac, &hdr, sizeof(hdr), TRUE);
3353 switch (cmd) {
3354 case CMD_PAUSE:
3355 pr_debug("%s:CMD_PAUSE\n", __func__);
3356 hdr.opcode = ASM_SESSION_CMD_PAUSE;
3357 state = &ac->cmd_state;
3358 break;
3359 case CMD_FLUSH:
3360 pr_debug("%s:CMD_FLUSH\n", __func__);
3361 hdr.opcode = ASM_STREAM_CMD_FLUSH;
3362 state = &ac->cmd_state;
3363 break;
3364 case CMD_OUT_FLUSH:
3365 pr_debug("%s:CMD_OUT_FLUSH\n", __func__);
3366 hdr.opcode = ASM_STREAM_CMD_FLUSH_READBUFS;
3367 state = &ac->cmd_state;
3368 break;
3369 case CMD_EOS:
3370 pr_debug("%s:CMD_EOS\n", __func__);
3371 hdr.opcode = ASM_DATA_CMD_EOS;
3372 atomic_set(&ac->cmd_state, 0);
3373 state = &ac->cmd_state;
3374 break;
3375 case CMD_CLOSE:
3376 pr_debug("%s:CMD_CLOSE\n", __func__);
3377 hdr.opcode = ASM_STREAM_CMD_CLOSE;
3378 state = &ac->cmd_state;
3379 break;
3380 default:
3381 pr_err("Invalid format[%d]\n", cmd);
3382 goto fail_cmd;
3383 }
3384 pr_debug("%s:session[%d]opcode[0x%x] ", __func__,
3385 ac->session,
3386 hdr.opcode);
3387 rc = apr_send_pkt(ac->apr, (uint32_t *) &hdr);
3388 if (rc < 0) {
3389 pr_err("Commmand 0x%x failed\n", hdr.opcode);
3390 goto fail_cmd;
3391 }
3392 rc = wait_event_timeout(ac->cmd_wait, (atomic_read(state) == 0), 5*HZ);
3393 if (!rc) {
3394 pr_err("timeout. waited for response opcode[0x%x]\n",
3395 hdr.opcode);
3396 goto fail_cmd;
3397 }
3398 if (cmd == CMD_FLUSH)
3399 q6asm_reset_buf_state(ac);
3400 if (cmd == CMD_CLOSE) {
3401 /* check if DSP return all buffers */
3402 if (ac->port[IN].buf) {
3403 for (cnt = 0; cnt < ac->port[IN].max_buf_cnt;
3404 cnt++) {
3405 if (ac->port[IN].buf[cnt].used == IN) {
3406 pr_debug("Write Buf[%d] not returned\n",
3407 cnt);
3408 }
3409 }
3410 }
3411 if (ac->port[OUT].buf) {
3412 for (cnt = 0; cnt < ac->port[OUT].max_buf_cnt; cnt++) {
3413 if (ac->port[OUT].buf[cnt].used == OUT) {
3414 pr_debug("Read Buf[%d] not returned\n",
3415 cnt);
3416 }
3417 }
3418 }
3419 }
3420 return 0;
3421fail_cmd:
3422 return -EINVAL;
3423}
3424
3425int q6asm_cmd_nowait(struct audio_client *ac, int cmd)
3426{
3427 struct apr_hdr hdr;
3428 int rc;
3429
3430 if (!ac || ac->apr == NULL) {
3431 pr_err("%s:APR handle NULL\n", __func__);
3432 return -EINVAL;
3433 }
3434 q6asm_add_hdr_async(ac, &hdr, sizeof(hdr), TRUE);
3435 switch (cmd) {
3436 case CMD_PAUSE:
3437 pr_debug("%s:CMD_PAUSE\n", __func__);
3438 hdr.opcode = ASM_SESSION_CMD_PAUSE;
3439 break;
3440 case CMD_EOS:
3441 pr_debug("%s:CMD_EOS\n", __func__);
3442 hdr.opcode = ASM_DATA_CMD_EOS;
3443 break;
3444 default:
3445 pr_err("%s:Invalid format[%d]\n", __func__, cmd);
3446 goto fail_cmd;
3447 }
3448 pr_debug("%s:session[%d]opcode[0x%x] ", __func__,
3449 ac->session,
3450 hdr.opcode);
3451 rc = apr_send_pkt(ac->apr, (uint32_t *) &hdr);
3452 if (rc < 0) {
3453 pr_err("%s:Commmand 0x%x failed\n", __func__, hdr.opcode);
3454 goto fail_cmd;
3455 }
3456 return 0;
3457fail_cmd:
3458 return -EINVAL;
3459}
3460
3461static void q6asm_reset_buf_state(struct audio_client *ac)
3462{
3463 int cnt = 0;
3464 int loopcnt = 0;
3465 struct audio_port_data *port = NULL;
3466
3467 if (ac->io_mode == SYNC_IO_MODE) {
3468 mutex_lock(&ac->cmd_lock);
3469 for (loopcnt = 0; loopcnt <= OUT; loopcnt++) {
3470 port = &ac->port[loopcnt];
3471 cnt = port->max_buf_cnt - 1;
3472 port->dsp_buf = 0;
3473 port->cpu_buf = 0;
3474 while (cnt >= 0) {
3475 if (!port->buf)
3476 continue;
3477 port->buf[cnt].used = 1;
3478 cnt--;
3479 }
3480 }
3481 mutex_unlock(&ac->cmd_lock);
3482 }
3483}
3484
3485int q6asm_reg_tx_overflow(struct audio_client *ac, uint16_t enable)
3486{
3487 struct asm_stream_cmd_reg_tx_overflow_event tx_overflow;
3488 int rc;
3489
3490 if (!ac || ac->apr == NULL) {
3491 pr_err("APR handle NULL\n");
3492 return -EINVAL;
3493 }
3494 pr_debug("%s:session[%d]enable[%d]\n", __func__,
3495 ac->session, enable);
3496 q6asm_add_hdr(ac, &tx_overflow.hdr, sizeof(tx_overflow), TRUE);
3497
3498 tx_overflow.hdr.opcode = \
3499 ASM_SESSION_CMD_REGISTER_FOR_TX_OVERFLOW_EVENTS;
3500 /* tx overflow event: enable */
3501 tx_overflow.enable = enable;
3502
3503 rc = apr_send_pkt(ac->apr, (uint32_t *) &tx_overflow);
3504 if (rc < 0) {
3505 pr_err("tx overflow op[0x%x]rc[%d]\n", \
3506 tx_overflow.hdr.opcode, rc);
3507 goto fail_cmd;
3508 }
3509 rc = wait_event_timeout(ac->cmd_wait,
3510 (atomic_read(&ac->cmd_state) == 0), 5*HZ);
3511 if (!rc) {
3512 pr_err("timeout. waited for tx overflow\n");
3513 goto fail_cmd;
3514 }
3515 return 0;
3516fail_cmd:
3517 return -EINVAL;
3518}
3519
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003520int q6asm_get_apr_service_id(int session_id)
3521{
3522 pr_debug("%s\n", __func__);
3523
Shiv Maliyappanahallia84982a2012-01-19 15:25:04 -08003524 if (session_id < 0 || session_id > SESSION_MAX) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003525 pr_err("%s: invalid session_id = %d\n", __func__, session_id);
3526 return -EINVAL;
3527 }
3528
3529 return ((struct apr_svc *)session[session_id]->apr)->id;
3530}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003531
3532
3533static int __init q6asm_init(void)
3534{
3535 pr_debug("%s\n", __func__);
3536 init_waitqueue_head(&this_mmap.cmd_wait);
3537 memset(session, 0, sizeof(session));
Rajesha Kini3498c932011-07-19 19:58:27 +05303538#ifdef CONFIG_DEBUG_FS
3539 out_buffer = kmalloc(OUT_BUFFER_SIZE, GFP_KERNEL);
3540 out_dentry = debugfs_create_file("audio_out_latency_measurement_node",\
3541 S_IFREG | S_IRUGO | S_IWUGO,\
3542 NULL, NULL, &audio_output_latency_debug_fops);
3543 if (IS_ERR(out_dentry))
3544 pr_err("debugfs_create_file failed\n");
3545 in_buffer = kmalloc(IN_BUFFER_SIZE, GFP_KERNEL);
3546 in_dentry = debugfs_create_file("audio_in_latency_measurement_node",\
3547 S_IFREG | S_IRUGO | S_IWUGO,\
3548 NULL, NULL, &audio_input_latency_debug_fops);
3549 if (IS_ERR(in_dentry))
3550 pr_err("debugfs_create_file failed\n");
3551#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003552 return 0;
3553}
3554
3555device_initcall(q6asm_init);