blob: 01e5f84ec41c66cbdcc58a7a3d924ffd432f8362 [file] [log] [blame]
Vatsal Bucha028b0062019-01-28 18:54:56 +05301/* Copyright (c) 2012-2017, 2019, The Linux Foundation. All rights reserved.
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13#include <linux/mutex.h>
14#include <linux/wait.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053015#include <linux/sched.h>
16#include <linux/spinlock.h>
17#include <linux/slab.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053018#include <dsp/msm_audio_ion.h>
19#include <dsp/apr_audio-v2.h>
20#include <ipc/apr_us.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053021#include "q6usm.h"
22
23#define ADSP_MEMORY_MAP_SHMEM8_4K_POOL 3
24
25#define MEM_4K_OFFSET 4095
26#define MEM_4K_MASK 0xfffff000
27
28#define USM_SESSION_MAX 0x02 /* aDSP:USM limit */
29
30#define READDONE_IDX_STATUS 0
31
32#define WRITEDONE_IDX_STATUS 0
33
34/* Standard timeout in the asynchronous ops */
35#define Q6USM_TIMEOUT_JIFFIES (1*HZ) /* 1 sec */
36
37static DEFINE_MUTEX(session_lock);
38
39static struct us_client *session[USM_SESSION_MAX];
40static int32_t q6usm_mmapcallback(struct apr_client_data *data, void *priv);
41static int32_t q6usm_callback(struct apr_client_data *data, void *priv);
42static void q6usm_add_hdr(struct us_client *usc, struct apr_hdr *hdr,
43 uint32_t pkt_size, bool cmd_flg);
44
45struct usm_mmap {
46 atomic_t ref_cnt;
47 atomic_t cmd_state;
48 wait_queue_head_t cmd_wait;
49 void *apr;
50 int mem_handle;
51};
52
53static struct usm_mmap this_mmap;
54
55static void q6usm_add_mmaphdr(struct apr_hdr *hdr,
56 uint32_t pkt_size, bool cmd_flg, u32 token)
57{
58 hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
59 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
60 hdr->src_port = 0;
61 hdr->dest_port = 0;
62 if (cmd_flg) {
63 hdr->token = token;
64 atomic_set(&this_mmap.cmd_state, 1);
65 }
66 hdr->pkt_size = pkt_size;
67}
68
69static int q6usm_memory_map(phys_addr_t buf_add, int dir, uint32_t bufsz,
70 uint32_t bufcnt, uint32_t session, uint32_t *mem_handle)
71{
72 struct usm_cmd_memory_map_region mem_region_map;
73 int rc = 0;
74
75 if (this_mmap.apr == NULL) {
76 pr_err("%s: APR handle NULL\n", __func__);
77 return -EINVAL;
78 }
79
80 q6usm_add_mmaphdr(&mem_region_map.hdr,
81 sizeof(struct usm_cmd_memory_map_region), true,
82 ((session << 8) | dir));
83
84 mem_region_map.hdr.opcode = USM_CMD_SHARED_MEM_MAP_REGION;
85 mem_region_map.mempool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
86
87 mem_region_map.num_regions = 1;
88 mem_region_map.flags = 0;
89
90 mem_region_map.shm_addr_lsw = lower_32_bits(buf_add);
91 mem_region_map.shm_addr_msw =
92 msm_audio_populate_upper_32_bits(buf_add);
93 mem_region_map.mem_size_bytes = bufsz * bufcnt;
94
95 rc = apr_send_pkt(this_mmap.apr, (uint32_t *) &mem_region_map);
96 if (rc < 0) {
97 pr_err("%s: mem_map op[0x%x]rc[%d]\n",
98 __func__, mem_region_map.hdr.opcode, rc);
99 rc = -EINVAL;
100 goto fail_cmd;
101 }
102
103 rc = wait_event_timeout(this_mmap.cmd_wait,
104 (atomic_read(&this_mmap.cmd_state) == 0),
105 Q6USM_TIMEOUT_JIFFIES);
106 if (!rc) {
107 rc = -ETIME;
108 pr_err("%s: timeout. waited for memory_map\n", __func__);
109 } else {
110 *mem_handle = this_mmap.mem_handle;
111 rc = 0;
112 }
113fail_cmd:
114 return rc;
115}
116
117int q6usm_memory_unmap(phys_addr_t buf_add, int dir, uint32_t session,
118 uint32_t mem_handle)
119{
120 struct usm_cmd_memory_unmap_region mem_unmap;
121 int rc = 0;
122
123 if (this_mmap.apr == NULL) {
124 pr_err("%s: APR handle NULL\n", __func__);
125 return -EINVAL;
126 }
127
128 q6usm_add_mmaphdr(&mem_unmap.hdr,
129 sizeof(struct usm_cmd_memory_unmap_region), true,
130 ((session << 8) | dir));
131 mem_unmap.hdr.opcode = USM_CMD_SHARED_MEM_UNMAP_REGION;
132 mem_unmap.mem_map_handle = mem_handle;
133
134 rc = apr_send_pkt(this_mmap.apr, (uint32_t *) &mem_unmap);
135 if (rc < 0) {
136 pr_err("%s: mem_unmap op[0x%x] rc[%d]\n",
137 __func__, mem_unmap.hdr.opcode, rc);
138 goto fail_cmd;
139 }
140
141 rc = wait_event_timeout(this_mmap.cmd_wait,
142 (atomic_read(&this_mmap.cmd_state) == 0),
143 Q6USM_TIMEOUT_JIFFIES);
144 if (!rc) {
145 rc = -ETIME;
146 pr_err("%s: timeout. waited for memory_unmap\n", __func__);
147 } else
148 rc = 0;
149fail_cmd:
150 return rc;
151}
152
153static int q6usm_session_alloc(struct us_client *usc)
154{
155 int ind = 0;
156
157 mutex_lock(&session_lock);
158 for (ind = 0; ind < USM_SESSION_MAX; ++ind) {
159 if (!session[ind]) {
160 session[ind] = usc;
161 mutex_unlock(&session_lock);
162 ++ind; /* session id: 0 reserved */
163 pr_debug("%s: session[%d] was allocated\n",
164 __func__, ind);
165 return ind;
166 }
167 }
168 mutex_unlock(&session_lock);
169 return -ENOMEM;
170}
171
172static void q6usm_session_free(struct us_client *usc)
173{
174 /* Session index was incremented during allocation */
175 uint16_t ind = (uint16_t)usc->session - 1;
176
177 pr_debug("%s: to free session[%d]\n", __func__, ind);
178 if (ind < USM_SESSION_MAX) {
179 mutex_lock(&session_lock);
180 session[ind] = NULL;
181 mutex_unlock(&session_lock);
182 }
183}
184
185static int q6usm_us_client_buf_free(unsigned int dir,
186 struct us_client *usc)
187{
188 struct us_port_data *port;
189 int rc = 0;
190
191 if ((usc == NULL) ||
192 ((dir != IN) && (dir != OUT)))
193 return -EINVAL;
194
195 mutex_lock(&usc->cmd_lock);
196 port = &usc->port[dir];
197 if (port == NULL) {
198 mutex_unlock(&usc->cmd_lock);
199 return -EINVAL;
200 }
201
202 if (port->data == NULL) {
203 mutex_unlock(&usc->cmd_lock);
204 return 0;
205 }
206
207 rc = q6usm_memory_unmap(port->phys, dir, usc->session,
208 *((uint32_t *)port->ext));
209 pr_debug("%s: data[%pK]phys[%llx][%pK]\n", __func__,
210 (void *)port->data, (u64)port->phys, (void *)&port->phys);
211
212 msm_audio_ion_free(port->client, port->handle);
213
214 port->data = NULL;
215 port->phys = 0;
216 port->buf_size = 0;
217 port->buf_cnt = 0;
218 port->client = NULL;
219 port->handle = NULL;
220
221 mutex_unlock(&usc->cmd_lock);
222 return rc;
223}
224
225int q6usm_us_param_buf_free(unsigned int dir,
226 struct us_client *usc)
227{
228 struct us_port_data *port;
229 int rc = 0;
230
231 if ((usc == NULL) ||
232 ((dir != IN) && (dir != OUT)))
233 return -EINVAL;
234
235 mutex_lock(&usc->cmd_lock);
236 port = &usc->port[dir];
237 if (port == NULL) {
238 mutex_unlock(&usc->cmd_lock);
239 return -EINVAL;
240 }
241
242 if (port->param_buf == NULL) {
243 mutex_unlock(&usc->cmd_lock);
244 return 0;
245 }
246
247 rc = q6usm_memory_unmap(port->param_phys, dir, usc->session,
248 *((uint32_t *)port->param_buf_mem_handle));
249 pr_debug("%s: data[%pK]phys[%llx][%pK]\n", __func__,
250 (void *)port->param_buf, (u64)port->param_phys,
251 (void *)&port->param_phys);
252
253 msm_audio_ion_free(port->param_client, port->param_handle);
254
255 port->param_buf = NULL;
256 port->param_phys = 0;
257 port->param_buf_size = 0;
258 port->param_client = NULL;
259 port->param_handle = NULL;
260
261 mutex_unlock(&usc->cmd_lock);
262 return rc;
263}
264
265void q6usm_us_client_free(struct us_client *usc)
266{
267 int loopcnt = 0;
268 struct us_port_data *port;
269 uint32_t *p_mem_handle = NULL;
270
271 if ((usc == NULL) ||
272 !(usc->session))
273 return;
274
275 for (loopcnt = 0; loopcnt <= OUT; ++loopcnt) {
276 port = &usc->port[loopcnt];
277 if (port->data == NULL)
278 continue;
279 pr_debug("%s: loopcnt = %d\n", __func__, loopcnt);
280 q6usm_us_client_buf_free(loopcnt, usc);
281 q6usm_us_param_buf_free(loopcnt, usc);
282 }
283 q6usm_session_free(usc);
284 apr_deregister(usc->apr);
285
286 pr_debug("%s: APR De-Register\n", __func__);
287
288 if (atomic_read(&this_mmap.ref_cnt) <= 0) {
289 pr_err("%s: APR Common Port Already Closed\n", __func__);
290 goto done;
291 }
292
293 atomic_dec(&this_mmap.ref_cnt);
294 if (atomic_read(&this_mmap.ref_cnt) == 0) {
295 apr_deregister(this_mmap.apr);
296 pr_debug("%s: APR De-Register common port\n", __func__);
297 }
298
299done:
300 p_mem_handle = (uint32_t *)usc->port[IN].ext;
301 kfree(p_mem_handle);
302 kfree(usc);
303 pr_debug("%s:\n", __func__);
304}
305
306struct us_client *q6usm_us_client_alloc(
307 void (*cb)(uint32_t, uint32_t, uint32_t *, void *),
308 void *priv)
309{
310 struct us_client *usc;
311 uint32_t *p_mem_handle = NULL;
312 int n;
313 int lcnt = 0;
314
315 usc = kzalloc(sizeof(struct us_client), GFP_KERNEL);
316 if (usc == NULL)
317 return NULL;
318
319 p_mem_handle = kzalloc(sizeof(uint32_t) * 4, GFP_KERNEL);
320 if (p_mem_handle == NULL) {
321 kfree(usc);
322 return NULL;
323 }
324
325 n = q6usm_session_alloc(usc);
326 if (n <= 0)
327 goto fail_session;
328 usc->session = n;
329 usc->cb = cb;
330 usc->priv = priv;
331 usc->apr = apr_register("ADSP", "USM",
332 (apr_fn)q6usm_callback,
333 ((usc->session) << 8 | 0x0001),
334 usc);
335
336 if (usc->apr == NULL) {
337 pr_err("%s: Registration with APR failed\n", __func__);
338 goto fail;
339 }
340 pr_debug("%s: Registering the common port with APR\n", __func__);
341 if (atomic_read(&this_mmap.ref_cnt) == 0) {
342 this_mmap.apr = apr_register("ADSP", "USM",
343 (apr_fn)q6usm_mmapcallback,
344 0x0FFFFFFFF, &this_mmap);
345 if (this_mmap.apr == NULL) {
346 pr_err("%s: USM port registration failed\n",
347 __func__);
348 goto fail;
349 }
350 }
351
352 atomic_inc(&this_mmap.ref_cnt);
353 init_waitqueue_head(&usc->cmd_wait);
354 mutex_init(&usc->cmd_lock);
355 for (lcnt = 0; lcnt <= OUT; ++lcnt) {
356 mutex_init(&usc->port[lcnt].lock);
357 spin_lock_init(&usc->port[lcnt].dsp_lock);
358 usc->port[lcnt].ext = (void *)p_mem_handle++;
359 usc->port[lcnt].param_buf_mem_handle = (void *)p_mem_handle++;
360 pr_err("%s: usc->port[%d].ext=%pK;\n",
361 __func__, lcnt, usc->port[lcnt].ext);
362 }
363 atomic_set(&usc->cmd_state, 0);
364
365 return usc;
366fail:
367 kfree(p_mem_handle);
368 q6usm_us_client_free(usc);
369 return NULL;
370fail_session:
371 kfree(p_mem_handle);
372 kfree(usc);
373 return NULL;
374}
375
376int q6usm_us_client_buf_alloc(unsigned int dir,
377 struct us_client *usc,
378 unsigned int bufsz,
379 unsigned int bufcnt)
380{
381 int rc = 0;
382 struct us_port_data *port = NULL;
383 unsigned int size = bufsz*bufcnt;
384 size_t len;
385
386 if ((usc == NULL) ||
387 ((dir != IN) && (dir != OUT)) || (size == 0) ||
388 (usc->session <= 0 || usc->session > USM_SESSION_MAX)) {
389 pr_err("%s: wrong parameters: size=%d; bufcnt=%d\n",
390 __func__, size, bufcnt);
391 return -EINVAL;
392 }
393
394 mutex_lock(&usc->cmd_lock);
395
396 port = &usc->port[dir];
397
398 /* The size to allocate should be multiple of 4K bytes */
399 size = PAGE_ALIGN(size);
400
401 rc = msm_audio_ion_alloc("ultrasound_client",
402 &port->client, &port->handle,
403 size, &port->phys,
404 &len, &port->data);
405
406 if (rc) {
407 pr_err("%s: US ION allocation failed, rc = %d\n",
408 __func__, rc);
409 mutex_unlock(&usc->cmd_lock);
410 return -ENOMEM;
411 }
412
413 port->buf_cnt = bufcnt;
414 port->buf_size = bufsz;
415 pr_debug("%s: data[%pK]; phys[%llx]; [%pK]\n", __func__,
416 (void *)port->data,
417 (u64)port->phys,
418 (void *)&port->phys);
419
420 rc = q6usm_memory_map(port->phys, dir, size, 1, usc->session,
421 (uint32_t *)port->ext);
422 if (rc < 0) {
423 pr_err("%s: CMD Memory_map failed\n", __func__);
424 mutex_unlock(&usc->cmd_lock);
425 q6usm_us_client_buf_free(dir, usc);
426 q6usm_us_param_buf_free(dir, usc);
427 } else {
428 mutex_unlock(&usc->cmd_lock);
429 rc = 0;
430 }
431
432 return rc;
433}
434
435int q6usm_us_param_buf_alloc(unsigned int dir,
436 struct us_client *usc,
437 unsigned int bufsz)
438{
439 int rc = 0;
440 struct us_port_data *port = NULL;
441 unsigned int size = bufsz;
442 size_t len;
443
444 if ((usc == NULL) ||
445 ((dir != IN) && (dir != OUT)) ||
446 (usc->session <= 0 || usc->session > USM_SESSION_MAX)) {
447 pr_err("%s: wrong parameters: direction=%d, bufsz=%d\n",
448 __func__, dir, bufsz);
449 return -EINVAL;
450 }
451
452 mutex_lock(&usc->cmd_lock);
453
454 port = &usc->port[dir];
455
456 if (bufsz == 0) {
457 pr_debug("%s: bufsz=0, get/set param commands are forbidden\n",
458 __func__);
459 port->param_buf = NULL;
460 mutex_unlock(&usc->cmd_lock);
461 return rc;
462 }
463
464 /* The size to allocate should be multiple of 4K bytes */
465 size = PAGE_ALIGN(size);
466
467 rc = msm_audio_ion_alloc("ultrasound_client",
468 &port->param_client, &port->param_handle,
469 size, &port->param_phys,
470 &len, &port->param_buf);
471
472 if (rc) {
473 pr_err("%s: US ION allocation failed, rc = %d\n",
474 __func__, rc);
475 mutex_unlock(&usc->cmd_lock);
476 return -ENOMEM;
477 }
478
479 port->param_buf_size = bufsz;
480 pr_debug("%s: param_buf[%pK]; param_phys[%llx]; [%pK]\n", __func__,
481 (void *)port->param_buf,
482 (u64)port->param_phys,
483 (void *)&port->param_phys);
484
485 rc = q6usm_memory_map(port->param_phys, (IN | OUT), size, 1,
486 usc->session, (uint32_t *)port->param_buf_mem_handle);
487 if (rc < 0) {
488 pr_err("%s: CMD Memory_map failed\n", __func__);
489 mutex_unlock(&usc->cmd_lock);
490 q6usm_us_client_buf_free(dir, usc);
491 q6usm_us_param_buf_free(dir, usc);
492 } else {
493 mutex_unlock(&usc->cmd_lock);
494 rc = 0;
495 }
496
497 return rc;
498}
499
500static int32_t q6usm_mmapcallback(struct apr_client_data *data, void *priv)
501{
502 uint32_t token;
503 uint32_t *payload = data->payload;
504
Vatsal Bucha028b0062019-01-28 18:54:56 +0530505 if (data->payload_size < (2 * sizeof(uint32_t))) {
506 pr_err("%s: payload has invalid size[%d]\n", __func__,
507 data->payload_size);
508 return -EINVAL;
509 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530510 pr_debug("%s: ptr0[0x%x]; ptr1[0x%x]; opcode[0x%x]\n",
511 __func__, payload[0], payload[1], data->opcode);
512 pr_debug("%s: token[0x%x]; payload_size[%d]; src[%d]; dest[%d];\n",
513 __func__, data->token, data->payload_size,
514 data->src_port, data->dest_port);
515
516 if (data->opcode == APR_BASIC_RSP_RESULT) {
517 /* status field check */
518 if (payload[1]) {
519 pr_err("%s: wrong response[%d] on cmd [%d]\n",
520 __func__, payload[1], payload[0]);
521 } else {
522 token = data->token;
523 switch (payload[0]) {
524 case USM_CMD_SHARED_MEM_UNMAP_REGION:
525 if (atomic_read(&this_mmap.cmd_state)) {
526 atomic_set(&this_mmap.cmd_state, 0);
527 wake_up(&this_mmap.cmd_wait);
528 }
529 /* fallthrough */
530 case USM_CMD_SHARED_MEM_MAP_REGION:
531 /* For MEM_MAP, additional answer is waited, */
532 /* therfore, no wake-up here */
533 pr_debug("%s: cmd[0x%x]; result[0x%x]\n",
534 __func__, payload[0], payload[1]);
535 break;
536 default:
537 pr_debug("%s: wrong command[0x%x]\n",
538 __func__, payload[0]);
539 break;
540 }
541 }
542 } else {
543 if (data->opcode == USM_CMDRSP_SHARED_MEM_MAP_REGION) {
544 this_mmap.mem_handle = payload[0];
545 pr_debug("%s: memory map handle = 0x%x",
546 __func__, payload[0]);
547 if (atomic_read(&this_mmap.cmd_state)) {
548 atomic_set(&this_mmap.cmd_state, 0);
549 wake_up(&this_mmap.cmd_wait);
550 }
551 }
552 }
553 return 0;
554}
555
556
557static int32_t q6usm_callback(struct apr_client_data *data, void *priv)
558{
559 struct us_client *usc = (struct us_client *)priv;
560 unsigned long dsp_flags;
561 uint32_t *payload = data->payload;
562 uint32_t token = data->token;
563 uint32_t opcode = Q6USM_EVENT_UNDEF;
564
565 if (usc == NULL) {
566 pr_err("%s: client info is NULL\n", __func__);
567 return -EINVAL;
568 }
569
570 if (data->opcode == APR_BASIC_RSP_RESULT) {
571 /* status field check */
572 if (payload[1]) {
573 pr_err("%s: wrong response[%d] on cmd [%d]\n",
574 __func__, payload[1], payload[0]);
575 if (usc->cb)
576 usc->cb(data->opcode, token,
577 (uint32_t *)data->payload, usc->priv);
578 } else {
579 switch (payload[0]) {
580 case USM_SESSION_CMD_RUN:
581 case USM_STREAM_CMD_CLOSE:
582 if (token != usc->session) {
583 pr_err("%s: wrong token[%d]",
584 __func__, token);
585 break;
586 }
587 case USM_STREAM_CMD_OPEN_READ:
588 case USM_STREAM_CMD_OPEN_WRITE:
589 case USM_STREAM_CMD_SET_ENC_PARAM:
590 case USM_DATA_CMD_MEDIA_FORMAT_UPDATE:
591 case USM_SESSION_CMD_SIGNAL_DETECT_MODE:
592 case USM_STREAM_CMD_SET_PARAM:
593 case USM_STREAM_CMD_GET_PARAM:
594 if (atomic_read(&usc->cmd_state)) {
595 atomic_set(&usc->cmd_state, 0);
596 wake_up(&usc->cmd_wait);
597 }
598 if (usc->cb)
599 usc->cb(data->opcode, token,
600 (uint32_t *)data->payload,
601 usc->priv);
602 break;
603 default:
604 break;
605 }
606 }
607 return 0;
608 }
609
610 switch (data->opcode) {
611 case RESET_EVENTS: {
612 pr_err("%s: Reset event is received: %d %d\n",
613 __func__,
614 data->reset_event,
615 data->reset_proc);
616
617 opcode = RESET_EVENTS;
618
619 apr_reset(this_mmap.apr);
620 this_mmap.apr = NULL;
621
622 apr_reset(usc->apr);
623 usc->apr = NULL;
624
625 break;
626 }
627
628
629 case USM_DATA_EVENT_READ_DONE: {
630 struct us_port_data *port = &usc->port[OUT];
631
632 opcode = Q6USM_EVENT_READ_DONE;
633 spin_lock_irqsave(&port->dsp_lock, dsp_flags);
634 if (payload[READDONE_IDX_STATUS]) {
635 pr_err("%s: wrong READDONE[%d]; token[%d]\n",
636 __func__,
637 payload[READDONE_IDX_STATUS],
638 token);
639 token = USM_WRONG_TOKEN;
640 spin_unlock_irqrestore(&port->dsp_lock,
641 dsp_flags);
642 break;
643 }
644
645 if (port->expected_token != token) {
646 u32 cpu_buf = port->cpu_buf;
647
648 pr_err("%s: expected[%d] != token[%d]\n",
649 __func__, port->expected_token, token);
650 pr_debug("%s: dsp_buf=%d; cpu_buf=%d;\n",
651 __func__, port->dsp_buf, cpu_buf);
652
653 token = USM_WRONG_TOKEN;
654 /* To prevent data handle continiue */
655 port->expected_token = USM_WRONG_TOKEN;
656 spin_unlock_irqrestore(&port->dsp_lock,
657 dsp_flags);
658 break;
659 } /* port->expected_token != data->token */
660
661 port->expected_token = token + 1;
662 if (port->expected_token == port->buf_cnt)
663 port->expected_token = 0;
664
665 /* gap support */
666 if (port->expected_token != port->cpu_buf) {
667 port->dsp_buf = port->expected_token;
668 token = port->dsp_buf; /* for callback */
669 } else
670 port->dsp_buf = token;
671
672 spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
673 break;
674 } /* case USM_DATA_EVENT_READ_DONE */
675
676 case USM_DATA_EVENT_WRITE_DONE: {
677 struct us_port_data *port = &usc->port[IN];
678
679 opcode = Q6USM_EVENT_WRITE_DONE;
680 if (payload[WRITEDONE_IDX_STATUS]) {
681 pr_err("%s: wrong WRITEDONE_IDX_STATUS[%d]\n",
682 __func__,
683 payload[WRITEDONE_IDX_STATUS]);
684 break;
685 }
686
687 spin_lock_irqsave(&port->dsp_lock, dsp_flags);
688 port->dsp_buf = token + 1;
689 if (port->dsp_buf == port->buf_cnt)
690 port->dsp_buf = 0;
691 spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
692
693 break;
694 } /* case USM_DATA_EVENT_WRITE_DONE */
695
696 case USM_SESSION_EVENT_SIGNAL_DETECT_RESULT: {
697 pr_debug("%s: US detect result: result=%d",
698 __func__,
699 payload[0]);
700 opcode = Q6USM_EVENT_SIGNAL_DETECT_RESULT;
701
702 break;
703 } /* case USM_SESSION_EVENT_SIGNAL_DETECT_RESULT */
704
705 default:
706 return 0;
707
708 } /* switch */
709
710 if (usc->cb)
711 usc->cb(opcode, token,
712 data->payload, usc->priv);
713
714 return 0;
715}
716
717uint32_t q6usm_get_virtual_address(int dir,
718 struct us_client *usc,
719 struct vm_area_struct *vms)
720{
721 uint32_t ret = 0xffffffff;
722
723 if (vms && (usc != NULL) && ((dir == IN) || (dir == OUT))) {
724 struct us_port_data *port = &usc->port[dir];
725 int size = PAGE_ALIGN(port->buf_size * port->buf_cnt);
726 struct audio_buffer ab;
727
728 ab.phys = port->phys;
729 ab.data = port->data;
730 ab.used = 1;
731 ab.size = size;
732 ab.actual_size = size;
733 ab.handle = port->handle;
734 ab.client = port->client;
735
736 ret = msm_audio_ion_mmap(&ab, vms);
737
738 }
739 return ret;
740}
741
742static void q6usm_add_hdr(struct us_client *usc, struct apr_hdr *hdr,
743 uint32_t pkt_size, bool cmd_flg)
744{
745 mutex_lock(&usc->cmd_lock);
746 hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
747 APR_HDR_LEN(sizeof(struct apr_hdr)),
748 APR_PKT_VER);
749 hdr->src_svc = ((struct apr_svc *)usc->apr)->id;
750 hdr->src_domain = APR_DOMAIN_APPS;
751 hdr->dest_svc = APR_SVC_USM;
752 hdr->dest_domain = APR_DOMAIN_ADSP;
753 hdr->src_port = (usc->session << 8) | 0x0001;
754 hdr->dest_port = (usc->session << 8) | 0x0001;
755 if (cmd_flg) {
756 hdr->token = usc->session;
757 atomic_set(&usc->cmd_state, 1);
758 }
759 hdr->pkt_size = pkt_size;
760 mutex_unlock(&usc->cmd_lock);
761}
762
763static uint32_t q6usm_ext2int_format(uint32_t ext_format)
764{
765 uint32_t int_format = INVALID_FORMAT;
766
767 switch (ext_format) {
768 case FORMAT_USPS_EPOS:
769 int_format = US_POINT_EPOS_FORMAT_V2;
770 break;
771 case FORMAT_USRAW:
772 int_format = US_RAW_FORMAT_V2;
773 break;
774 case FORMAT_USPROX:
775 int_format = US_PROX_FORMAT_V4;
776 break;
777 case FORMAT_USGES_SYNC:
778 int_format = US_GES_SYNC_FORMAT;
779 break;
780 case FORMAT_USRAW_SYNC:
781 int_format = US_RAW_SYNC_FORMAT;
782 break;
783 default:
784 pr_err("%s: Invalid format[%d]\n", __func__, ext_format);
785 break;
786 }
787
788 return int_format;
789}
790
791int q6usm_open_read(struct us_client *usc,
792 uint32_t format)
793{
794 uint32_t int_format = INVALID_FORMAT;
795 int rc = 0x00;
796 struct usm_stream_cmd_open_read open;
797
798 if ((usc == NULL) || (usc->apr == NULL)) {
799 pr_err("%s: client or its apr is NULL\n", __func__);
800 return -EINVAL;
801 }
802
803 pr_debug("%s: session[%d]", __func__, usc->session);
804
805 q6usm_add_hdr(usc, &open.hdr, sizeof(open), true);
806 open.hdr.opcode = USM_STREAM_CMD_OPEN_READ;
807 open.src_endpoint = 0; /* AFE */
808 open.pre_proc_top = 0; /* No preprocessing required */
809
810 int_format = q6usm_ext2int_format(format);
811 if (int_format == INVALID_FORMAT)
812 return -EINVAL;
813
814 open.uMode = STREAM_PRIORITY_NORMAL;
815 open.format = int_format;
816
817 rc = apr_send_pkt(usc->apr, (uint32_t *) &open);
818 if (rc < 0) {
819 pr_err("%s: open failed op[0x%x]rc[%d]\n",
820 __func__, open.hdr.opcode, rc);
821 goto fail_cmd;
822 }
823 rc = wait_event_timeout(usc->cmd_wait,
824 (atomic_read(&usc->cmd_state) == 0),
825 Q6USM_TIMEOUT_JIFFIES);
826 if (!rc) {
827 rc = -ETIME;
828 pr_err("%s: timeout, waited for OPEN_READ rc[%d]\n",
829 __func__, rc);
830 goto fail_cmd;
831 } else
832 rc = 0;
833fail_cmd:
834 return rc;
835}
836
837
838int q6usm_enc_cfg_blk(struct us_client *usc, struct us_encdec_cfg *us_cfg)
839{
840 uint32_t int_format = INVALID_FORMAT;
841 struct usm_stream_cmd_encdec_cfg_blk enc_cfg_obj;
842 struct usm_stream_cmd_encdec_cfg_blk *enc_cfg = &enc_cfg_obj;
843 int rc = 0;
844 uint32_t total_cfg_size =
845 sizeof(struct usm_stream_cmd_encdec_cfg_blk);
846 uint32_t round_params_size = 0;
847 uint8_t is_allocated = 0;
848
849
850 if ((usc == NULL) || (us_cfg == NULL)) {
851 pr_err("%s: wrong input", __func__);
852 return -EINVAL;
853 }
854
855 int_format = q6usm_ext2int_format(us_cfg->format_id);
856 if (int_format == INVALID_FORMAT) {
857 pr_err("%s: wrong input format[%d]",
858 __func__, us_cfg->format_id);
859 return -EINVAL;
860 }
861
862 /* Transparent configuration data is after enc_cfg */
863 /* Integer number of u32s is required */
864 round_params_size = ((us_cfg->params_size + 3)/4) * 4;
865 if (round_params_size > USM_MAX_CFG_DATA_SIZE) {
866 /* Dynamic allocated encdec_cfg_blk is required */
867 /* static part use */
868 round_params_size -= USM_MAX_CFG_DATA_SIZE;
869 total_cfg_size += round_params_size;
870 enc_cfg = kzalloc(total_cfg_size, GFP_KERNEL);
871 if (enc_cfg == NULL) {
872 pr_err("%s: enc_cfg[%d] allocation failed\n",
873 __func__, total_cfg_size);
874 return -ENOMEM;
875 }
876 is_allocated = 1;
877 } else
878 round_params_size = 0;
879
880 q6usm_add_hdr(usc, &enc_cfg->hdr, total_cfg_size, true);
881
882 enc_cfg->hdr.opcode = USM_STREAM_CMD_SET_ENC_PARAM;
883 enc_cfg->param_id = USM_PARAM_ID_ENCDEC_ENC_CFG_BLK;
884 enc_cfg->param_size = sizeof(struct usm_encode_cfg_blk)+
885 round_params_size;
886 enc_cfg->enc_blk.frames_per_buf = 1;
887 enc_cfg->enc_blk.format_id = int_format;
888 enc_cfg->enc_blk.cfg_size = sizeof(struct usm_cfg_common)+
889 USM_MAX_CFG_DATA_SIZE +
890 round_params_size;
891 memcpy(&(enc_cfg->enc_blk.cfg_common), &(us_cfg->cfg_common),
892 sizeof(struct usm_cfg_common));
893
894 /* Transparent data copy */
895 memcpy(enc_cfg->enc_blk.transp_data, us_cfg->params,
896 us_cfg->params_size);
897 pr_debug("%s: cfg_size[%d], params_size[%d]\n",
898 __func__,
899 enc_cfg->enc_blk.cfg_size,
900 us_cfg->params_size);
901 pr_debug("%s: params[%d,%d,%d,%d, %d,%d,%d,%d]\n",
902 __func__,
903 enc_cfg->enc_blk.transp_data[0],
904 enc_cfg->enc_blk.transp_data[1],
905 enc_cfg->enc_blk.transp_data[2],
906 enc_cfg->enc_blk.transp_data[3],
907 enc_cfg->enc_blk.transp_data[4],
908 enc_cfg->enc_blk.transp_data[5],
909 enc_cfg->enc_blk.transp_data[6],
910 enc_cfg->enc_blk.transp_data[7]
911 );
912 pr_debug("%s: srate:%d, ch=%d, bps= %d;\n",
913 __func__, enc_cfg->enc_blk.cfg_common.sample_rate,
914 enc_cfg->enc_blk.cfg_common.ch_cfg,
915 enc_cfg->enc_blk.cfg_common.bits_per_sample);
916 pr_debug("dmap:[0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x]; dev_id=0x%x\n",
917 enc_cfg->enc_blk.cfg_common.data_map[0],
918 enc_cfg->enc_blk.cfg_common.data_map[1],
919 enc_cfg->enc_blk.cfg_common.data_map[2],
920 enc_cfg->enc_blk.cfg_common.data_map[3],
921 enc_cfg->enc_blk.cfg_common.data_map[4],
922 enc_cfg->enc_blk.cfg_common.data_map[5],
923 enc_cfg->enc_blk.cfg_common.data_map[6],
924 enc_cfg->enc_blk.cfg_common.data_map[7],
925 enc_cfg->enc_blk.cfg_common.dev_id);
926
927 rc = apr_send_pkt(usc->apr, (uint32_t *) enc_cfg);
928 if (rc < 0) {
929 pr_err("%s:Comamnd open failed\n", __func__);
930 rc = -EINVAL;
931 goto fail_cmd;
932 }
933 rc = wait_event_timeout(usc->cmd_wait,
934 (atomic_read(&usc->cmd_state) == 0),
935 Q6USM_TIMEOUT_JIFFIES);
936 if (!rc) {
937 rc = -ETIME;
938 pr_err("%s: timeout opcode[0x%x]\n",
939 __func__, enc_cfg->hdr.opcode);
940 } else
941 rc = 0;
942
943fail_cmd:
944 if (is_allocated == 1)
945 kfree(enc_cfg);
946
947 return rc;
948}
949
950int q6usm_dec_cfg_blk(struct us_client *usc, struct us_encdec_cfg *us_cfg)
951{
952
953 uint32_t int_format = INVALID_FORMAT;
954 struct usm_stream_media_format_update dec_cfg_obj;
955 struct usm_stream_media_format_update *dec_cfg = &dec_cfg_obj;
956
957 int rc = 0;
958 uint32_t total_cfg_size = sizeof(struct usm_stream_media_format_update);
959 uint32_t round_params_size = 0;
960 uint8_t is_allocated = 0;
961
962
963 if ((usc == NULL) || (us_cfg == NULL)) {
964 pr_err("%s: wrong input", __func__);
965 return -EINVAL;
966 }
967
968 int_format = q6usm_ext2int_format(us_cfg->format_id);
969 if (int_format == INVALID_FORMAT) {
970 pr_err("%s: wrong input format[%d]",
971 __func__, us_cfg->format_id);
972 return -EINVAL;
973 }
974
975 /* Transparent configuration data is after enc_cfg */
976 /* Integer number of u32s is required */
977 round_params_size = ((us_cfg->params_size + 3)/4) * 4;
978 if (round_params_size > USM_MAX_CFG_DATA_SIZE) {
979 /* Dynamic allocated encdec_cfg_blk is required */
980 /* static part use */
981 round_params_size -= USM_MAX_CFG_DATA_SIZE;
982 total_cfg_size += round_params_size;
983 dec_cfg = kzalloc(total_cfg_size, GFP_KERNEL);
984 if (dec_cfg == NULL) {
985 pr_err("%s:dec_cfg[%d] allocation failed\n",
986 __func__, total_cfg_size);
987 return -ENOMEM;
988 }
989 is_allocated = 1;
990 } else { /* static transp_data is enough */
991 round_params_size = 0;
992 }
993
994 q6usm_add_hdr(usc, &dec_cfg->hdr, total_cfg_size, true);
995
996 dec_cfg->hdr.opcode = USM_DATA_CMD_MEDIA_FORMAT_UPDATE;
997 dec_cfg->format_id = int_format;
998 dec_cfg->cfg_size = sizeof(struct usm_cfg_common) +
999 USM_MAX_CFG_DATA_SIZE +
1000 round_params_size;
1001 memcpy(&(dec_cfg->cfg_common), &(us_cfg->cfg_common),
1002 sizeof(struct usm_cfg_common));
1003 /* Transparent data copy */
1004 memcpy(dec_cfg->transp_data, us_cfg->params, us_cfg->params_size);
1005 pr_debug("%s: cfg_size[%d], params_size[%d]; parambytes[%d,%d,%d,%d]\n",
1006 __func__,
1007 dec_cfg->cfg_size,
1008 us_cfg->params_size,
1009 dec_cfg->transp_data[0],
1010 dec_cfg->transp_data[1],
1011 dec_cfg->transp_data[2],
1012 dec_cfg->transp_data[3]
1013 );
1014
1015 rc = apr_send_pkt(usc->apr, (uint32_t *) dec_cfg);
1016 if (rc < 0) {
1017 pr_err("%s:Comamnd open failed\n", __func__);
1018 rc = -EINVAL;
1019 goto fail_cmd;
1020 }
1021 rc = wait_event_timeout(usc->cmd_wait,
1022 (atomic_read(&usc->cmd_state) == 0),
1023 Q6USM_TIMEOUT_JIFFIES);
1024 if (!rc) {
1025 rc = -ETIME;
1026 pr_err("%s: timeout opcode[0x%x]\n",
1027 __func__, dec_cfg->hdr.opcode);
1028 } else
1029 rc = 0;
1030
1031fail_cmd:
1032 if (is_allocated == 1)
1033 kfree(dec_cfg);
1034
1035 return rc;
1036}
1037
1038int q6usm_open_write(struct us_client *usc,
1039 uint32_t format)
1040{
1041 int rc = 0;
1042 uint32_t int_format = INVALID_FORMAT;
1043 struct usm_stream_cmd_open_write open;
1044
1045 if ((usc == NULL) || (usc->apr == NULL)) {
1046 pr_err("%s: APR handle NULL\n", __func__);
1047 return -EINVAL;
1048 }
1049
1050 pr_debug("%s: session[%d]", __func__, usc->session);
1051
1052 q6usm_add_hdr(usc, &open.hdr, sizeof(open), true);
1053 open.hdr.opcode = USM_STREAM_CMD_OPEN_WRITE;
1054
1055 int_format = q6usm_ext2int_format(format);
1056 if (int_format == INVALID_FORMAT) {
1057 pr_err("%s: wrong format[%d]", __func__, format);
1058 return -EINVAL;
1059 }
1060
1061 open.format = int_format;
1062
1063 rc = apr_send_pkt(usc->apr, (uint32_t *) &open);
1064 if (rc < 0) {
1065 pr_err("%s:open failed op[0x%x]rc[%d]\n",
1066 __func__, open.hdr.opcode, rc);
1067 goto fail_cmd;
1068 }
1069 rc = wait_event_timeout(usc->cmd_wait,
1070 (atomic_read(&usc->cmd_state) == 0),
1071 Q6USM_TIMEOUT_JIFFIES);
1072 if (!rc) {
1073 rc = -ETIME;
1074 pr_err("%s:timeout. waited for OPEN_WRITR rc[%d]\n",
1075 __func__, rc);
1076 goto fail_cmd;
1077 } else
1078 rc = 0;
1079
1080fail_cmd:
1081 return rc;
1082}
1083
1084int q6usm_run(struct us_client *usc, uint32_t flags,
1085 uint32_t msw_ts, uint32_t lsw_ts)
1086{
1087 struct usm_stream_cmd_run run;
1088 int rc = 0;
1089
1090 if ((usc == NULL) || (usc->apr == NULL)) {
1091 pr_err("%s: APR handle NULL\n", __func__);
1092 return -EINVAL;
1093 }
1094 q6usm_add_hdr(usc, &run.hdr, sizeof(run), true);
1095
1096 run.hdr.opcode = USM_SESSION_CMD_RUN;
1097 run.flags = flags;
1098 run.msw_ts = msw_ts;
1099 run.lsw_ts = lsw_ts;
1100
1101 rc = apr_send_pkt(usc->apr, (uint32_t *) &run);
1102 if (rc < 0) {
1103 pr_err("%s: Commmand run failed[%d]\n", __func__, rc);
1104 goto fail_cmd;
1105 }
1106
1107 rc = wait_event_timeout(usc->cmd_wait,
1108 (atomic_read(&usc->cmd_state) == 0),
1109 Q6USM_TIMEOUT_JIFFIES);
1110 if (!rc) {
1111 rc = -ETIME;
1112 pr_err("%s: timeout. waited for run success rc[%d]\n",
1113 __func__, rc);
1114 } else
1115 rc = 0;
1116
1117fail_cmd:
1118 return rc;
1119}
1120
1121
1122
1123int q6usm_read(struct us_client *usc, uint32_t read_ind)
1124{
1125 struct usm_stream_cmd_read read;
1126 struct us_port_data *port = NULL;
1127 int rc = 0;
1128 u32 read_counter = 0;
1129 u32 loop_ind = 0;
1130 u64 buf_addr = 0;
1131
1132 if ((usc == NULL) || (usc->apr == NULL)) {
1133 pr_err("%s: APR handle NULL\n", __func__);
1134 return -EINVAL;
1135 }
1136 port = &usc->port[OUT];
1137
1138 if (read_ind > port->buf_cnt) {
1139 pr_err("%s: wrong read_ind[%d]\n",
1140 __func__, read_ind);
1141 return -EINVAL;
1142 }
1143 if (read_ind == port->cpu_buf) {
1144 pr_err("%s: no free region\n", __func__);
1145 return 0;
1146 }
1147
1148 if (read_ind > port->cpu_buf) { /* 1 range */
1149 read_counter = read_ind - port->cpu_buf;
1150 } else { /* 2 ranges */
1151 read_counter = (port->buf_cnt - port->cpu_buf) + read_ind;
1152 }
1153
1154 q6usm_add_hdr(usc, &read.hdr, sizeof(read), false);
1155
1156 read.hdr.opcode = USM_DATA_CMD_READ;
1157 read.buf_size = port->buf_size;
1158 buf_addr = (u64)(port->phys) + port->buf_size * (port->cpu_buf);
1159 read.buf_addr_lsw = lower_32_bits(buf_addr);
1160 read.buf_addr_msw = msm_audio_populate_upper_32_bits(buf_addr);
1161 read.mem_map_handle = *((uint32_t *)(port->ext));
1162
1163 for (loop_ind = 0; loop_ind < read_counter; ++loop_ind) {
1164 u32 temp_cpu_buf = port->cpu_buf;
1165
1166 buf_addr = (u64)(port->phys) +
1167 port->buf_size * (port->cpu_buf);
1168 read.buf_addr_lsw = lower_32_bits(buf_addr);
1169 read.buf_addr_msw = msm_audio_populate_upper_32_bits(buf_addr);
1170 read.seq_id = port->cpu_buf;
1171 read.hdr.token = port->cpu_buf;
1172 read.counter = 1;
1173
1174 ++(port->cpu_buf);
1175 if (port->cpu_buf == port->buf_cnt)
1176 port->cpu_buf = 0;
1177
1178 rc = apr_send_pkt(usc->apr, (uint32_t *) &read);
1179
1180 if (rc < 0) {
1181 port->cpu_buf = temp_cpu_buf;
1182
1183 pr_err("%s:read op[0x%x]rc[%d]\n",
1184 __func__, read.hdr.opcode, rc);
1185 break;
1186 }
1187
1188 rc = 0;
1189 } /* bufs loop */
1190
1191 return rc;
1192}
1193
1194int q6usm_write(struct us_client *usc, uint32_t write_ind)
1195{
1196 int rc = 0;
1197 struct usm_stream_cmd_write cmd_write;
1198 struct us_port_data *port = NULL;
1199 u32 current_dsp_buf = 0;
1200 u64 buf_addr = 0;
1201
1202 if ((usc == NULL) || (usc->apr == NULL)) {
1203 pr_err("%s: APR handle NULL\n", __func__);
1204 return -EINVAL;
1205 }
1206 port = &usc->port[IN];
1207
1208 current_dsp_buf = port->dsp_buf;
1209 /* free region, caused by new dsp_buf report from DSP, */
1210 /* can be only extended */
1211 if (port->cpu_buf >= current_dsp_buf) {
1212 /* 2 -part free region, including empty buffer */
1213 if ((write_ind <= port->cpu_buf) &&
1214 (write_ind > current_dsp_buf)) {
1215 pr_err("%s: wrong w_ind[%d]; d_buf=%d; c_buf=%d\n",
1216 __func__, write_ind,
1217 current_dsp_buf, port->cpu_buf);
1218 return -EINVAL;
1219 }
1220 } else {
1221 /* 1 -part free region */
1222 if ((write_ind <= port->cpu_buf) ||
1223 (write_ind > current_dsp_buf)) {
1224 pr_err("%s: wrong w_ind[%d]; d_buf=%d; c_buf=%d\n",
1225 __func__, write_ind,
1226 current_dsp_buf, port->cpu_buf);
1227 return -EINVAL;
1228 }
1229 }
1230
1231 q6usm_add_hdr(usc, &cmd_write.hdr, sizeof(cmd_write), false);
1232
1233 cmd_write.hdr.opcode = USM_DATA_CMD_WRITE;
1234 cmd_write.buf_size = port->buf_size;
1235 buf_addr = (u64)(port->phys) + port->buf_size * (port->cpu_buf);
1236 cmd_write.buf_addr_lsw = lower_32_bits(buf_addr);
1237 cmd_write.buf_addr_msw = msm_audio_populate_upper_32_bits(buf_addr);
1238 cmd_write.mem_map_handle = *((uint32_t *)(port->ext));
1239 cmd_write.res0 = 0;
1240 cmd_write.res1 = 0;
1241 cmd_write.res2 = 0;
1242
1243 while (port->cpu_buf != write_ind) {
1244 u32 temp_cpu_buf = port->cpu_buf;
1245
1246 buf_addr = (u64)(port->phys) +
1247 port->buf_size * (port->cpu_buf);
1248 cmd_write.buf_addr_lsw = lower_32_bits(buf_addr);
1249 cmd_write.buf_addr_msw =
1250 msm_audio_populate_upper_32_bits(buf_addr);
1251 cmd_write.seq_id = port->cpu_buf;
1252 cmd_write.hdr.token = port->cpu_buf;
1253
1254 ++(port->cpu_buf);
1255 if (port->cpu_buf == port->buf_cnt)
1256 port->cpu_buf = 0;
1257
1258 rc = apr_send_pkt(usc->apr, (uint32_t *) &cmd_write);
1259
1260 if (rc < 0) {
1261 port->cpu_buf = temp_cpu_buf;
1262 pr_err("%s:write op[0x%x];rc[%d];cpu_buf[%d]\n",
1263 __func__, cmd_write.hdr.opcode,
1264 rc, port->cpu_buf);
1265 break;
1266 }
1267
1268 rc = 0;
1269 }
1270
1271 return rc;
1272}
1273
1274bool q6usm_is_write_buf_full(struct us_client *usc, uint32_t *free_region)
1275{
1276 struct us_port_data *port = NULL;
1277 u32 cpu_buf = 0;
1278
1279 if ((usc == NULL) || !free_region) {
1280 pr_err("%s: input data wrong\n", __func__);
1281 return false;
1282 }
1283 port = &usc->port[IN];
1284 cpu_buf = port->cpu_buf + 1;
1285 if (cpu_buf == port->buf_cnt)
1286 cpu_buf = 0;
1287
1288 *free_region = port->dsp_buf;
1289
1290 return cpu_buf == *free_region;
1291}
1292
1293int q6usm_cmd(struct us_client *usc, int cmd)
1294{
1295 struct apr_hdr hdr;
1296 int rc = 0;
1297 atomic_t *state;
1298
1299 if ((usc == NULL) || (usc->apr == NULL)) {
1300 pr_err("%s: APR handle NULL\n", __func__);
1301 return -EINVAL;
1302 }
1303 q6usm_add_hdr(usc, &hdr, sizeof(hdr), true);
1304 switch (cmd) {
1305 case CMD_CLOSE:
1306 hdr.opcode = USM_STREAM_CMD_CLOSE;
1307 state = &usc->cmd_state;
1308 break;
1309
1310 default:
1311 pr_err("%s:Invalid format[%d]\n", __func__, cmd);
1312 goto fail_cmd;
1313 }
1314
1315 rc = apr_send_pkt(usc->apr, (uint32_t *) &hdr);
1316 if (rc < 0) {
1317 pr_err("%s: Command 0x%x failed\n", __func__, hdr.opcode);
1318 goto fail_cmd;
1319 }
1320 rc = wait_event_timeout(usc->cmd_wait, (atomic_read(state) == 0),
1321 Q6USM_TIMEOUT_JIFFIES);
1322 if (!rc) {
1323 rc = -ETIME;
1324 pr_err("%s:timeout. waited for response opcode[0x%x]\n",
1325 __func__, hdr.opcode);
1326 } else
1327 rc = 0;
1328fail_cmd:
1329 return rc;
1330}
1331
1332int q6usm_set_us_detection(struct us_client *usc,
1333 struct usm_session_cmd_detect_info *detect_info,
1334 uint16_t detect_info_size)
1335{
1336 int rc = 0;
1337
1338 if ((usc == NULL) ||
1339 (detect_info_size == 0) ||
1340 (detect_info == NULL)) {
1341 pr_err("%s: wrong input: usc=0x%pK, inf_size=%d; info=0x%pK",
1342 __func__,
1343 usc,
1344 detect_info_size,
1345 detect_info);
1346 return -EINVAL;
1347 }
1348
1349 q6usm_add_hdr(usc, &detect_info->hdr, detect_info_size, true);
1350
1351 detect_info->hdr.opcode = USM_SESSION_CMD_SIGNAL_DETECT_MODE;
1352
1353 rc = apr_send_pkt(usc->apr, (uint32_t *)detect_info);
1354 if (rc < 0) {
1355 pr_err("%s:Comamnd signal detect failed\n", __func__);
1356 return -EINVAL;
1357 }
1358 rc = wait_event_timeout(usc->cmd_wait,
1359 (atomic_read(&usc->cmd_state) == 0),
1360 Q6USM_TIMEOUT_JIFFIES);
1361 if (!rc) {
1362 rc = -ETIME;
1363 pr_err("%s: CMD_SIGNAL_DETECT_MODE: timeout=%d\n",
1364 __func__, Q6USM_TIMEOUT_JIFFIES);
1365 } else
1366 rc = 0;
1367
1368 return rc;
1369}
1370
1371int q6usm_set_us_stream_param(int dir, struct us_client *usc,
1372 uint32_t module_id, uint32_t param_id, uint32_t buf_size)
1373{
1374 int rc = 0;
1375 struct usm_stream_cmd_set_param cmd_set_param;
1376 struct us_port_data *port = NULL;
1377
1378 if ((usc == NULL) || (usc->apr == NULL)) {
1379 pr_err("%s: APR handle NULL\n", __func__);
1380 return -EINVAL;
1381 }
1382 port = &usc->port[dir];
1383
1384 q6usm_add_hdr(usc, &cmd_set_param.hdr, sizeof(cmd_set_param), true);
1385
1386 cmd_set_param.hdr.opcode = USM_STREAM_CMD_SET_PARAM;
1387 cmd_set_param.buf_size = buf_size;
1388 cmd_set_param.buf_addr_msw =
1389 msm_audio_populate_upper_32_bits(port->param_phys);
1390 cmd_set_param.buf_addr_lsw = lower_32_bits(port->param_phys);
1391 cmd_set_param.mem_map_handle =
1392 *((uint32_t *)(port->param_buf_mem_handle));
1393 cmd_set_param.module_id = module_id;
1394 cmd_set_param.param_id = param_id;
1395 cmd_set_param.hdr.token = 0;
1396
1397 rc = apr_send_pkt(usc->apr, (uint32_t *) &cmd_set_param);
1398
1399 if (rc < 0) {
1400 pr_err("%s:write op[0x%x];rc[%d]\n",
1401 __func__, cmd_set_param.hdr.opcode, rc);
1402 }
1403
1404 rc = wait_event_timeout(usc->cmd_wait,
1405 (atomic_read(&usc->cmd_state) == 0),
1406 Q6USM_TIMEOUT_JIFFIES);
1407 if (!rc) {
1408 rc = -ETIME;
1409 pr_err("%s: CMD_SET_PARAM: timeout=%d\n",
1410 __func__, Q6USM_TIMEOUT_JIFFIES);
1411 } else
1412 rc = 0;
1413
1414 return rc;
1415}
1416
1417int q6usm_get_us_stream_param(int dir, struct us_client *usc,
1418 uint32_t module_id, uint32_t param_id, uint32_t buf_size)
1419{
1420 int rc = 0;
1421 struct usm_stream_cmd_get_param cmd_get_param;
1422 struct us_port_data *port = NULL;
1423
1424 if ((usc == NULL) || (usc->apr == NULL)) {
1425 pr_err("%s: APR handle NULL\n", __func__);
1426 return -EINVAL;
1427 }
1428 port = &usc->port[dir];
1429
1430 q6usm_add_hdr(usc, &cmd_get_param.hdr, sizeof(cmd_get_param), true);
1431
1432 cmd_get_param.hdr.opcode = USM_STREAM_CMD_GET_PARAM;
1433 cmd_get_param.buf_size = buf_size;
1434 cmd_get_param.buf_addr_msw =
1435 msm_audio_populate_upper_32_bits(port->param_phys);
1436 cmd_get_param.buf_addr_lsw = lower_32_bits(port->param_phys);
1437 cmd_get_param.mem_map_handle =
1438 *((uint32_t *)(port->param_buf_mem_handle));
1439 cmd_get_param.module_id = module_id;
1440 cmd_get_param.param_id = param_id;
1441 cmd_get_param.hdr.token = 0;
1442
1443 rc = apr_send_pkt(usc->apr, (uint32_t *) &cmd_get_param);
1444
1445 if (rc < 0) {
1446 pr_err("%s:write op[0x%x];rc[%d]\n",
1447 __func__, cmd_get_param.hdr.opcode, rc);
1448 }
1449
1450 rc = wait_event_timeout(usc->cmd_wait,
1451 (atomic_read(&usc->cmd_state) == 0),
1452 Q6USM_TIMEOUT_JIFFIES);
1453 if (!rc) {
1454 rc = -ETIME;
1455 pr_err("%s: CMD_GET_PARAM: timeout=%d\n",
1456 __func__, Q6USM_TIMEOUT_JIFFIES);
1457 } else
1458 rc = 0;
1459
1460 return rc;
1461}
1462
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301463int __init q6usm_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301464{
1465 pr_debug("%s\n", __func__);
1466 init_waitqueue_head(&this_mmap.cmd_wait);
1467 memset(session, 0, sizeof(session));
1468 return 0;
1469}