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