blob: fc76dc33762d6e2ae67309f6ae9f855ec99556f7 [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) {
Vatsal Bucha46737022019-01-28 18:44:29 +0530571 if (data->payload_size < (2 * sizeof(uint32_t))) {
572 pr_err("%s: payload has invalid size[%d]\n", __func__,
573 data->payload_size);
574 return -EINVAL;
575 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530576 /* status field check */
577 if (payload[1]) {
578 pr_err("%s: wrong response[%d] on cmd [%d]\n",
579 __func__, payload[1], payload[0]);
580 if (usc->cb)
581 usc->cb(data->opcode, token,
582 (uint32_t *)data->payload, usc->priv);
583 } else {
584 switch (payload[0]) {
585 case USM_SESSION_CMD_RUN:
586 case USM_STREAM_CMD_CLOSE:
587 if (token != usc->session) {
588 pr_err("%s: wrong token[%d]",
589 __func__, token);
590 break;
591 }
592 case USM_STREAM_CMD_OPEN_READ:
593 case USM_STREAM_CMD_OPEN_WRITE:
594 case USM_STREAM_CMD_SET_ENC_PARAM:
595 case USM_DATA_CMD_MEDIA_FORMAT_UPDATE:
596 case USM_SESSION_CMD_SIGNAL_DETECT_MODE:
597 case USM_STREAM_CMD_SET_PARAM:
598 case USM_STREAM_CMD_GET_PARAM:
599 if (atomic_read(&usc->cmd_state)) {
600 atomic_set(&usc->cmd_state, 0);
601 wake_up(&usc->cmd_wait);
602 }
603 if (usc->cb)
604 usc->cb(data->opcode, token,
605 (uint32_t *)data->payload,
606 usc->priv);
607 break;
608 default:
609 break;
610 }
611 }
612 return 0;
613 }
614
615 switch (data->opcode) {
616 case RESET_EVENTS: {
617 pr_err("%s: Reset event is received: %d %d\n",
618 __func__,
619 data->reset_event,
620 data->reset_proc);
621
622 opcode = RESET_EVENTS;
623
624 apr_reset(this_mmap.apr);
625 this_mmap.apr = NULL;
626
627 apr_reset(usc->apr);
628 usc->apr = NULL;
629
630 break;
631 }
632
633
634 case USM_DATA_EVENT_READ_DONE: {
635 struct us_port_data *port = &usc->port[OUT];
636
637 opcode = Q6USM_EVENT_READ_DONE;
638 spin_lock_irqsave(&port->dsp_lock, dsp_flags);
Vatsal Bucha46737022019-01-28 18:44:29 +0530639 if (data->payload_size <
640 (sizeof(uint32_t)*(READDONE_IDX_STATUS + 1))) {
641 pr_err("%s: Invalid payload size for READDONE[%d]\n",
642 __func__, data->payload_size);
643 spin_unlock_irqrestore(&port->dsp_lock,
644 dsp_flags);
645 return -EINVAL;
646 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530647 if (payload[READDONE_IDX_STATUS]) {
648 pr_err("%s: wrong READDONE[%d]; token[%d]\n",
649 __func__,
650 payload[READDONE_IDX_STATUS],
651 token);
652 token = USM_WRONG_TOKEN;
653 spin_unlock_irqrestore(&port->dsp_lock,
654 dsp_flags);
655 break;
656 }
657
658 if (port->expected_token != token) {
659 u32 cpu_buf = port->cpu_buf;
660
661 pr_err("%s: expected[%d] != token[%d]\n",
662 __func__, port->expected_token, token);
663 pr_debug("%s: dsp_buf=%d; cpu_buf=%d;\n",
664 __func__, port->dsp_buf, cpu_buf);
665
666 token = USM_WRONG_TOKEN;
667 /* To prevent data handle continiue */
668 port->expected_token = USM_WRONG_TOKEN;
669 spin_unlock_irqrestore(&port->dsp_lock,
670 dsp_flags);
671 break;
672 } /* port->expected_token != data->token */
673
674 port->expected_token = token + 1;
675 if (port->expected_token == port->buf_cnt)
676 port->expected_token = 0;
677
678 /* gap support */
679 if (port->expected_token != port->cpu_buf) {
680 port->dsp_buf = port->expected_token;
681 token = port->dsp_buf; /* for callback */
682 } else
683 port->dsp_buf = token;
684
685 spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
686 break;
687 } /* case USM_DATA_EVENT_READ_DONE */
688
689 case USM_DATA_EVENT_WRITE_DONE: {
690 struct us_port_data *port = &usc->port[IN];
691
692 opcode = Q6USM_EVENT_WRITE_DONE;
Vatsal Bucha46737022019-01-28 18:44:29 +0530693 if (data->payload_size <
694 (sizeof(uint32_t)*(WRITEDONE_IDX_STATUS + 1))) {
695 pr_err("%s: Invalid payload size for WRITEDONE[%d]\n",
696 __func__, data->payload_size);
697 return -EINVAL;
698 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530699 if (payload[WRITEDONE_IDX_STATUS]) {
700 pr_err("%s: wrong WRITEDONE_IDX_STATUS[%d]\n",
701 __func__,
702 payload[WRITEDONE_IDX_STATUS]);
703 break;
704 }
705
706 spin_lock_irqsave(&port->dsp_lock, dsp_flags);
707 port->dsp_buf = token + 1;
708 if (port->dsp_buf == port->buf_cnt)
709 port->dsp_buf = 0;
710 spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
711
712 break;
713 } /* case USM_DATA_EVENT_WRITE_DONE */
714
715 case USM_SESSION_EVENT_SIGNAL_DETECT_RESULT: {
716 pr_debug("%s: US detect result: result=%d",
717 __func__,
718 payload[0]);
719 opcode = Q6USM_EVENT_SIGNAL_DETECT_RESULT;
720
721 break;
722 } /* case USM_SESSION_EVENT_SIGNAL_DETECT_RESULT */
723
724 default:
725 return 0;
726
727 } /* switch */
728
729 if (usc->cb)
730 usc->cb(opcode, token,
731 data->payload, usc->priv);
732
733 return 0;
734}
735
736uint32_t q6usm_get_virtual_address(int dir,
737 struct us_client *usc,
738 struct vm_area_struct *vms)
739{
740 uint32_t ret = 0xffffffff;
741
742 if (vms && (usc != NULL) && ((dir == IN) || (dir == OUT))) {
743 struct us_port_data *port = &usc->port[dir];
744 int size = PAGE_ALIGN(port->buf_size * port->buf_cnt);
745 struct audio_buffer ab;
746
747 ab.phys = port->phys;
748 ab.data = port->data;
749 ab.used = 1;
750 ab.size = size;
751 ab.actual_size = size;
752 ab.handle = port->handle;
753 ab.client = port->client;
754
755 ret = msm_audio_ion_mmap(&ab, vms);
756
757 }
758 return ret;
759}
760
761static void q6usm_add_hdr(struct us_client *usc, struct apr_hdr *hdr,
762 uint32_t pkt_size, bool cmd_flg)
763{
764 mutex_lock(&usc->cmd_lock);
765 hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
766 APR_HDR_LEN(sizeof(struct apr_hdr)),
767 APR_PKT_VER);
768 hdr->src_svc = ((struct apr_svc *)usc->apr)->id;
769 hdr->src_domain = APR_DOMAIN_APPS;
770 hdr->dest_svc = APR_SVC_USM;
771 hdr->dest_domain = APR_DOMAIN_ADSP;
772 hdr->src_port = (usc->session << 8) | 0x0001;
773 hdr->dest_port = (usc->session << 8) | 0x0001;
774 if (cmd_flg) {
775 hdr->token = usc->session;
776 atomic_set(&usc->cmd_state, 1);
777 }
778 hdr->pkt_size = pkt_size;
779 mutex_unlock(&usc->cmd_lock);
780}
781
782static uint32_t q6usm_ext2int_format(uint32_t ext_format)
783{
784 uint32_t int_format = INVALID_FORMAT;
785
786 switch (ext_format) {
787 case FORMAT_USPS_EPOS:
788 int_format = US_POINT_EPOS_FORMAT_V2;
789 break;
790 case FORMAT_USRAW:
791 int_format = US_RAW_FORMAT_V2;
792 break;
793 case FORMAT_USPROX:
794 int_format = US_PROX_FORMAT_V4;
795 break;
796 case FORMAT_USGES_SYNC:
797 int_format = US_GES_SYNC_FORMAT;
798 break;
799 case FORMAT_USRAW_SYNC:
800 int_format = US_RAW_SYNC_FORMAT;
801 break;
802 default:
803 pr_err("%s: Invalid format[%d]\n", __func__, ext_format);
804 break;
805 }
806
807 return int_format;
808}
809
810int q6usm_open_read(struct us_client *usc,
811 uint32_t format)
812{
813 uint32_t int_format = INVALID_FORMAT;
814 int rc = 0x00;
815 struct usm_stream_cmd_open_read open;
816
817 if ((usc == NULL) || (usc->apr == NULL)) {
818 pr_err("%s: client or its apr is NULL\n", __func__);
819 return -EINVAL;
820 }
821
822 pr_debug("%s: session[%d]", __func__, usc->session);
823
824 q6usm_add_hdr(usc, &open.hdr, sizeof(open), true);
825 open.hdr.opcode = USM_STREAM_CMD_OPEN_READ;
826 open.src_endpoint = 0; /* AFE */
827 open.pre_proc_top = 0; /* No preprocessing required */
828
829 int_format = q6usm_ext2int_format(format);
830 if (int_format == INVALID_FORMAT)
831 return -EINVAL;
832
833 open.uMode = STREAM_PRIORITY_NORMAL;
834 open.format = int_format;
835
836 rc = apr_send_pkt(usc->apr, (uint32_t *) &open);
837 if (rc < 0) {
838 pr_err("%s: open failed op[0x%x]rc[%d]\n",
839 __func__, open.hdr.opcode, rc);
840 goto fail_cmd;
841 }
842 rc = wait_event_timeout(usc->cmd_wait,
843 (atomic_read(&usc->cmd_state) == 0),
844 Q6USM_TIMEOUT_JIFFIES);
845 if (!rc) {
846 rc = -ETIME;
847 pr_err("%s: timeout, waited for OPEN_READ rc[%d]\n",
848 __func__, rc);
849 goto fail_cmd;
850 } else
851 rc = 0;
852fail_cmd:
853 return rc;
854}
855
856
857int q6usm_enc_cfg_blk(struct us_client *usc, struct us_encdec_cfg *us_cfg)
858{
859 uint32_t int_format = INVALID_FORMAT;
860 struct usm_stream_cmd_encdec_cfg_blk enc_cfg_obj;
861 struct usm_stream_cmd_encdec_cfg_blk *enc_cfg = &enc_cfg_obj;
862 int rc = 0;
863 uint32_t total_cfg_size =
864 sizeof(struct usm_stream_cmd_encdec_cfg_blk);
865 uint32_t round_params_size = 0;
866 uint8_t is_allocated = 0;
867
868
869 if ((usc == NULL) || (us_cfg == NULL)) {
870 pr_err("%s: wrong input", __func__);
871 return -EINVAL;
872 }
873
874 int_format = q6usm_ext2int_format(us_cfg->format_id);
875 if (int_format == INVALID_FORMAT) {
876 pr_err("%s: wrong input format[%d]",
877 __func__, us_cfg->format_id);
878 return -EINVAL;
879 }
880
881 /* Transparent configuration data is after enc_cfg */
882 /* Integer number of u32s is required */
883 round_params_size = ((us_cfg->params_size + 3)/4) * 4;
884 if (round_params_size > USM_MAX_CFG_DATA_SIZE) {
885 /* Dynamic allocated encdec_cfg_blk is required */
886 /* static part use */
887 round_params_size -= USM_MAX_CFG_DATA_SIZE;
888 total_cfg_size += round_params_size;
889 enc_cfg = kzalloc(total_cfg_size, GFP_KERNEL);
890 if (enc_cfg == NULL) {
891 pr_err("%s: enc_cfg[%d] allocation failed\n",
892 __func__, total_cfg_size);
893 return -ENOMEM;
894 }
895 is_allocated = 1;
896 } else
897 round_params_size = 0;
898
899 q6usm_add_hdr(usc, &enc_cfg->hdr, total_cfg_size, true);
900
901 enc_cfg->hdr.opcode = USM_STREAM_CMD_SET_ENC_PARAM;
902 enc_cfg->param_id = USM_PARAM_ID_ENCDEC_ENC_CFG_BLK;
903 enc_cfg->param_size = sizeof(struct usm_encode_cfg_blk)+
904 round_params_size;
905 enc_cfg->enc_blk.frames_per_buf = 1;
906 enc_cfg->enc_blk.format_id = int_format;
907 enc_cfg->enc_blk.cfg_size = sizeof(struct usm_cfg_common)+
908 USM_MAX_CFG_DATA_SIZE +
909 round_params_size;
910 memcpy(&(enc_cfg->enc_blk.cfg_common), &(us_cfg->cfg_common),
911 sizeof(struct usm_cfg_common));
912
913 /* Transparent data copy */
914 memcpy(enc_cfg->enc_blk.transp_data, us_cfg->params,
915 us_cfg->params_size);
916 pr_debug("%s: cfg_size[%d], params_size[%d]\n",
917 __func__,
918 enc_cfg->enc_blk.cfg_size,
919 us_cfg->params_size);
920 pr_debug("%s: params[%d,%d,%d,%d, %d,%d,%d,%d]\n",
921 __func__,
922 enc_cfg->enc_blk.transp_data[0],
923 enc_cfg->enc_blk.transp_data[1],
924 enc_cfg->enc_blk.transp_data[2],
925 enc_cfg->enc_blk.transp_data[3],
926 enc_cfg->enc_blk.transp_data[4],
927 enc_cfg->enc_blk.transp_data[5],
928 enc_cfg->enc_blk.transp_data[6],
929 enc_cfg->enc_blk.transp_data[7]
930 );
931 pr_debug("%s: srate:%d, ch=%d, bps= %d;\n",
932 __func__, enc_cfg->enc_blk.cfg_common.sample_rate,
933 enc_cfg->enc_blk.cfg_common.ch_cfg,
934 enc_cfg->enc_blk.cfg_common.bits_per_sample);
935 pr_debug("dmap:[0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x]; dev_id=0x%x\n",
936 enc_cfg->enc_blk.cfg_common.data_map[0],
937 enc_cfg->enc_blk.cfg_common.data_map[1],
938 enc_cfg->enc_blk.cfg_common.data_map[2],
939 enc_cfg->enc_blk.cfg_common.data_map[3],
940 enc_cfg->enc_blk.cfg_common.data_map[4],
941 enc_cfg->enc_blk.cfg_common.data_map[5],
942 enc_cfg->enc_blk.cfg_common.data_map[6],
943 enc_cfg->enc_blk.cfg_common.data_map[7],
944 enc_cfg->enc_blk.cfg_common.dev_id);
945
946 rc = apr_send_pkt(usc->apr, (uint32_t *) enc_cfg);
947 if (rc < 0) {
948 pr_err("%s:Comamnd open failed\n", __func__);
949 rc = -EINVAL;
950 goto fail_cmd;
951 }
952 rc = wait_event_timeout(usc->cmd_wait,
953 (atomic_read(&usc->cmd_state) == 0),
954 Q6USM_TIMEOUT_JIFFIES);
955 if (!rc) {
956 rc = -ETIME;
957 pr_err("%s: timeout opcode[0x%x]\n",
958 __func__, enc_cfg->hdr.opcode);
959 } else
960 rc = 0;
961
962fail_cmd:
963 if (is_allocated == 1)
964 kfree(enc_cfg);
965
966 return rc;
967}
968
969int q6usm_dec_cfg_blk(struct us_client *usc, struct us_encdec_cfg *us_cfg)
970{
971
972 uint32_t int_format = INVALID_FORMAT;
973 struct usm_stream_media_format_update dec_cfg_obj;
974 struct usm_stream_media_format_update *dec_cfg = &dec_cfg_obj;
975
976 int rc = 0;
977 uint32_t total_cfg_size = sizeof(struct usm_stream_media_format_update);
978 uint32_t round_params_size = 0;
979 uint8_t is_allocated = 0;
980
981
982 if ((usc == NULL) || (us_cfg == NULL)) {
983 pr_err("%s: wrong input", __func__);
984 return -EINVAL;
985 }
986
987 int_format = q6usm_ext2int_format(us_cfg->format_id);
988 if (int_format == INVALID_FORMAT) {
989 pr_err("%s: wrong input format[%d]",
990 __func__, us_cfg->format_id);
991 return -EINVAL;
992 }
993
994 /* Transparent configuration data is after enc_cfg */
995 /* Integer number of u32s is required */
996 round_params_size = ((us_cfg->params_size + 3)/4) * 4;
997 if (round_params_size > USM_MAX_CFG_DATA_SIZE) {
998 /* Dynamic allocated encdec_cfg_blk is required */
999 /* static part use */
1000 round_params_size -= USM_MAX_CFG_DATA_SIZE;
1001 total_cfg_size += round_params_size;
1002 dec_cfg = kzalloc(total_cfg_size, GFP_KERNEL);
1003 if (dec_cfg == NULL) {
1004 pr_err("%s:dec_cfg[%d] allocation failed\n",
1005 __func__, total_cfg_size);
1006 return -ENOMEM;
1007 }
1008 is_allocated = 1;
1009 } else { /* static transp_data is enough */
1010 round_params_size = 0;
1011 }
1012
1013 q6usm_add_hdr(usc, &dec_cfg->hdr, total_cfg_size, true);
1014
1015 dec_cfg->hdr.opcode = USM_DATA_CMD_MEDIA_FORMAT_UPDATE;
1016 dec_cfg->format_id = int_format;
1017 dec_cfg->cfg_size = sizeof(struct usm_cfg_common) +
1018 USM_MAX_CFG_DATA_SIZE +
1019 round_params_size;
1020 memcpy(&(dec_cfg->cfg_common), &(us_cfg->cfg_common),
1021 sizeof(struct usm_cfg_common));
1022 /* Transparent data copy */
1023 memcpy(dec_cfg->transp_data, us_cfg->params, us_cfg->params_size);
1024 pr_debug("%s: cfg_size[%d], params_size[%d]; parambytes[%d,%d,%d,%d]\n",
1025 __func__,
1026 dec_cfg->cfg_size,
1027 us_cfg->params_size,
1028 dec_cfg->transp_data[0],
1029 dec_cfg->transp_data[1],
1030 dec_cfg->transp_data[2],
1031 dec_cfg->transp_data[3]
1032 );
1033
1034 rc = apr_send_pkt(usc->apr, (uint32_t *) dec_cfg);
1035 if (rc < 0) {
1036 pr_err("%s:Comamnd open failed\n", __func__);
1037 rc = -EINVAL;
1038 goto fail_cmd;
1039 }
1040 rc = wait_event_timeout(usc->cmd_wait,
1041 (atomic_read(&usc->cmd_state) == 0),
1042 Q6USM_TIMEOUT_JIFFIES);
1043 if (!rc) {
1044 rc = -ETIME;
1045 pr_err("%s: timeout opcode[0x%x]\n",
1046 __func__, dec_cfg->hdr.opcode);
1047 } else
1048 rc = 0;
1049
1050fail_cmd:
1051 if (is_allocated == 1)
1052 kfree(dec_cfg);
1053
1054 return rc;
1055}
1056
1057int q6usm_open_write(struct us_client *usc,
1058 uint32_t format)
1059{
1060 int rc = 0;
1061 uint32_t int_format = INVALID_FORMAT;
1062 struct usm_stream_cmd_open_write open;
1063
1064 if ((usc == NULL) || (usc->apr == NULL)) {
1065 pr_err("%s: APR handle NULL\n", __func__);
1066 return -EINVAL;
1067 }
1068
1069 pr_debug("%s: session[%d]", __func__, usc->session);
1070
1071 q6usm_add_hdr(usc, &open.hdr, sizeof(open), true);
1072 open.hdr.opcode = USM_STREAM_CMD_OPEN_WRITE;
1073
1074 int_format = q6usm_ext2int_format(format);
1075 if (int_format == INVALID_FORMAT) {
1076 pr_err("%s: wrong format[%d]", __func__, format);
1077 return -EINVAL;
1078 }
1079
1080 open.format = int_format;
1081
1082 rc = apr_send_pkt(usc->apr, (uint32_t *) &open);
1083 if (rc < 0) {
1084 pr_err("%s:open failed op[0x%x]rc[%d]\n",
1085 __func__, open.hdr.opcode, rc);
1086 goto fail_cmd;
1087 }
1088 rc = wait_event_timeout(usc->cmd_wait,
1089 (atomic_read(&usc->cmd_state) == 0),
1090 Q6USM_TIMEOUT_JIFFIES);
1091 if (!rc) {
1092 rc = -ETIME;
1093 pr_err("%s:timeout. waited for OPEN_WRITR rc[%d]\n",
1094 __func__, rc);
1095 goto fail_cmd;
1096 } else
1097 rc = 0;
1098
1099fail_cmd:
1100 return rc;
1101}
1102
1103int q6usm_run(struct us_client *usc, uint32_t flags,
1104 uint32_t msw_ts, uint32_t lsw_ts)
1105{
1106 struct usm_stream_cmd_run run;
1107 int rc = 0;
1108
1109 if ((usc == NULL) || (usc->apr == NULL)) {
1110 pr_err("%s: APR handle NULL\n", __func__);
1111 return -EINVAL;
1112 }
1113 q6usm_add_hdr(usc, &run.hdr, sizeof(run), true);
1114
1115 run.hdr.opcode = USM_SESSION_CMD_RUN;
1116 run.flags = flags;
1117 run.msw_ts = msw_ts;
1118 run.lsw_ts = lsw_ts;
1119
1120 rc = apr_send_pkt(usc->apr, (uint32_t *) &run);
1121 if (rc < 0) {
1122 pr_err("%s: Commmand run failed[%d]\n", __func__, rc);
1123 goto fail_cmd;
1124 }
1125
1126 rc = wait_event_timeout(usc->cmd_wait,
1127 (atomic_read(&usc->cmd_state) == 0),
1128 Q6USM_TIMEOUT_JIFFIES);
1129 if (!rc) {
1130 rc = -ETIME;
1131 pr_err("%s: timeout. waited for run success rc[%d]\n",
1132 __func__, rc);
1133 } else
1134 rc = 0;
1135
1136fail_cmd:
1137 return rc;
1138}
1139
1140
1141
1142int q6usm_read(struct us_client *usc, uint32_t read_ind)
1143{
1144 struct usm_stream_cmd_read read;
1145 struct us_port_data *port = NULL;
1146 int rc = 0;
1147 u32 read_counter = 0;
1148 u32 loop_ind = 0;
1149 u64 buf_addr = 0;
1150
1151 if ((usc == NULL) || (usc->apr == NULL)) {
1152 pr_err("%s: APR handle NULL\n", __func__);
1153 return -EINVAL;
1154 }
1155 port = &usc->port[OUT];
1156
1157 if (read_ind > port->buf_cnt) {
1158 pr_err("%s: wrong read_ind[%d]\n",
1159 __func__, read_ind);
1160 return -EINVAL;
1161 }
1162 if (read_ind == port->cpu_buf) {
1163 pr_err("%s: no free region\n", __func__);
1164 return 0;
1165 }
1166
1167 if (read_ind > port->cpu_buf) { /* 1 range */
1168 read_counter = read_ind - port->cpu_buf;
1169 } else { /* 2 ranges */
1170 read_counter = (port->buf_cnt - port->cpu_buf) + read_ind;
1171 }
1172
1173 q6usm_add_hdr(usc, &read.hdr, sizeof(read), false);
1174
1175 read.hdr.opcode = USM_DATA_CMD_READ;
1176 read.buf_size = port->buf_size;
1177 buf_addr = (u64)(port->phys) + port->buf_size * (port->cpu_buf);
1178 read.buf_addr_lsw = lower_32_bits(buf_addr);
1179 read.buf_addr_msw = msm_audio_populate_upper_32_bits(buf_addr);
1180 read.mem_map_handle = *((uint32_t *)(port->ext));
1181
1182 for (loop_ind = 0; loop_ind < read_counter; ++loop_ind) {
1183 u32 temp_cpu_buf = port->cpu_buf;
1184
1185 buf_addr = (u64)(port->phys) +
1186 port->buf_size * (port->cpu_buf);
1187 read.buf_addr_lsw = lower_32_bits(buf_addr);
1188 read.buf_addr_msw = msm_audio_populate_upper_32_bits(buf_addr);
1189 read.seq_id = port->cpu_buf;
1190 read.hdr.token = port->cpu_buf;
1191 read.counter = 1;
1192
1193 ++(port->cpu_buf);
1194 if (port->cpu_buf == port->buf_cnt)
1195 port->cpu_buf = 0;
1196
1197 rc = apr_send_pkt(usc->apr, (uint32_t *) &read);
1198
1199 if (rc < 0) {
1200 port->cpu_buf = temp_cpu_buf;
1201
1202 pr_err("%s:read op[0x%x]rc[%d]\n",
1203 __func__, read.hdr.opcode, rc);
1204 break;
1205 }
1206
1207 rc = 0;
1208 } /* bufs loop */
1209
1210 return rc;
1211}
1212
1213int q6usm_write(struct us_client *usc, uint32_t write_ind)
1214{
1215 int rc = 0;
1216 struct usm_stream_cmd_write cmd_write;
1217 struct us_port_data *port = NULL;
1218 u32 current_dsp_buf = 0;
1219 u64 buf_addr = 0;
1220
1221 if ((usc == NULL) || (usc->apr == NULL)) {
1222 pr_err("%s: APR handle NULL\n", __func__);
1223 return -EINVAL;
1224 }
1225 port = &usc->port[IN];
1226
1227 current_dsp_buf = port->dsp_buf;
1228 /* free region, caused by new dsp_buf report from DSP, */
1229 /* can be only extended */
1230 if (port->cpu_buf >= current_dsp_buf) {
1231 /* 2 -part free region, including empty buffer */
1232 if ((write_ind <= port->cpu_buf) &&
1233 (write_ind > current_dsp_buf)) {
1234 pr_err("%s: wrong w_ind[%d]; d_buf=%d; c_buf=%d\n",
1235 __func__, write_ind,
1236 current_dsp_buf, port->cpu_buf);
1237 return -EINVAL;
1238 }
1239 } else {
1240 /* 1 -part free region */
1241 if ((write_ind <= port->cpu_buf) ||
1242 (write_ind > current_dsp_buf)) {
1243 pr_err("%s: wrong w_ind[%d]; d_buf=%d; c_buf=%d\n",
1244 __func__, write_ind,
1245 current_dsp_buf, port->cpu_buf);
1246 return -EINVAL;
1247 }
1248 }
1249
1250 q6usm_add_hdr(usc, &cmd_write.hdr, sizeof(cmd_write), false);
1251
1252 cmd_write.hdr.opcode = USM_DATA_CMD_WRITE;
1253 cmd_write.buf_size = port->buf_size;
1254 buf_addr = (u64)(port->phys) + port->buf_size * (port->cpu_buf);
1255 cmd_write.buf_addr_lsw = lower_32_bits(buf_addr);
1256 cmd_write.buf_addr_msw = msm_audio_populate_upper_32_bits(buf_addr);
1257 cmd_write.mem_map_handle = *((uint32_t *)(port->ext));
1258 cmd_write.res0 = 0;
1259 cmd_write.res1 = 0;
1260 cmd_write.res2 = 0;
1261
1262 while (port->cpu_buf != write_ind) {
1263 u32 temp_cpu_buf = port->cpu_buf;
1264
1265 buf_addr = (u64)(port->phys) +
1266 port->buf_size * (port->cpu_buf);
1267 cmd_write.buf_addr_lsw = lower_32_bits(buf_addr);
1268 cmd_write.buf_addr_msw =
1269 msm_audio_populate_upper_32_bits(buf_addr);
1270 cmd_write.seq_id = port->cpu_buf;
1271 cmd_write.hdr.token = port->cpu_buf;
1272
1273 ++(port->cpu_buf);
1274 if (port->cpu_buf == port->buf_cnt)
1275 port->cpu_buf = 0;
1276
1277 rc = apr_send_pkt(usc->apr, (uint32_t *) &cmd_write);
1278
1279 if (rc < 0) {
1280 port->cpu_buf = temp_cpu_buf;
1281 pr_err("%s:write op[0x%x];rc[%d];cpu_buf[%d]\n",
1282 __func__, cmd_write.hdr.opcode,
1283 rc, port->cpu_buf);
1284 break;
1285 }
1286
1287 rc = 0;
1288 }
1289
1290 return rc;
1291}
1292
1293bool q6usm_is_write_buf_full(struct us_client *usc, uint32_t *free_region)
1294{
1295 struct us_port_data *port = NULL;
1296 u32 cpu_buf = 0;
1297
1298 if ((usc == NULL) || !free_region) {
1299 pr_err("%s: input data wrong\n", __func__);
1300 return false;
1301 }
1302 port = &usc->port[IN];
1303 cpu_buf = port->cpu_buf + 1;
1304 if (cpu_buf == port->buf_cnt)
1305 cpu_buf = 0;
1306
1307 *free_region = port->dsp_buf;
1308
1309 return cpu_buf == *free_region;
1310}
1311
1312int q6usm_cmd(struct us_client *usc, int cmd)
1313{
1314 struct apr_hdr hdr;
1315 int rc = 0;
1316 atomic_t *state;
1317
1318 if ((usc == NULL) || (usc->apr == NULL)) {
1319 pr_err("%s: APR handle NULL\n", __func__);
1320 return -EINVAL;
1321 }
1322 q6usm_add_hdr(usc, &hdr, sizeof(hdr), true);
1323 switch (cmd) {
1324 case CMD_CLOSE:
1325 hdr.opcode = USM_STREAM_CMD_CLOSE;
1326 state = &usc->cmd_state;
1327 break;
1328
1329 default:
1330 pr_err("%s:Invalid format[%d]\n", __func__, cmd);
1331 goto fail_cmd;
1332 }
1333
1334 rc = apr_send_pkt(usc->apr, (uint32_t *) &hdr);
1335 if (rc < 0) {
1336 pr_err("%s: Command 0x%x failed\n", __func__, hdr.opcode);
1337 goto fail_cmd;
1338 }
1339 rc = wait_event_timeout(usc->cmd_wait, (atomic_read(state) == 0),
1340 Q6USM_TIMEOUT_JIFFIES);
1341 if (!rc) {
1342 rc = -ETIME;
1343 pr_err("%s:timeout. waited for response opcode[0x%x]\n",
1344 __func__, hdr.opcode);
1345 } else
1346 rc = 0;
1347fail_cmd:
1348 return rc;
1349}
1350
1351int q6usm_set_us_detection(struct us_client *usc,
1352 struct usm_session_cmd_detect_info *detect_info,
1353 uint16_t detect_info_size)
1354{
1355 int rc = 0;
1356
1357 if ((usc == NULL) ||
1358 (detect_info_size == 0) ||
1359 (detect_info == NULL)) {
1360 pr_err("%s: wrong input: usc=0x%pK, inf_size=%d; info=0x%pK",
1361 __func__,
1362 usc,
1363 detect_info_size,
1364 detect_info);
1365 return -EINVAL;
1366 }
1367
1368 q6usm_add_hdr(usc, &detect_info->hdr, detect_info_size, true);
1369
1370 detect_info->hdr.opcode = USM_SESSION_CMD_SIGNAL_DETECT_MODE;
1371
1372 rc = apr_send_pkt(usc->apr, (uint32_t *)detect_info);
1373 if (rc < 0) {
1374 pr_err("%s:Comamnd signal detect failed\n", __func__);
1375 return -EINVAL;
1376 }
1377 rc = wait_event_timeout(usc->cmd_wait,
1378 (atomic_read(&usc->cmd_state) == 0),
1379 Q6USM_TIMEOUT_JIFFIES);
1380 if (!rc) {
1381 rc = -ETIME;
1382 pr_err("%s: CMD_SIGNAL_DETECT_MODE: timeout=%d\n",
1383 __func__, Q6USM_TIMEOUT_JIFFIES);
1384 } else
1385 rc = 0;
1386
1387 return rc;
1388}
1389
1390int q6usm_set_us_stream_param(int dir, struct us_client *usc,
1391 uint32_t module_id, uint32_t param_id, uint32_t buf_size)
1392{
1393 int rc = 0;
1394 struct usm_stream_cmd_set_param cmd_set_param;
1395 struct us_port_data *port = NULL;
1396
1397 if ((usc == NULL) || (usc->apr == NULL)) {
1398 pr_err("%s: APR handle NULL\n", __func__);
1399 return -EINVAL;
1400 }
1401 port = &usc->port[dir];
1402
1403 q6usm_add_hdr(usc, &cmd_set_param.hdr, sizeof(cmd_set_param), true);
1404
1405 cmd_set_param.hdr.opcode = USM_STREAM_CMD_SET_PARAM;
1406 cmd_set_param.buf_size = buf_size;
1407 cmd_set_param.buf_addr_msw =
1408 msm_audio_populate_upper_32_bits(port->param_phys);
1409 cmd_set_param.buf_addr_lsw = lower_32_bits(port->param_phys);
1410 cmd_set_param.mem_map_handle =
1411 *((uint32_t *)(port->param_buf_mem_handle));
1412 cmd_set_param.module_id = module_id;
1413 cmd_set_param.param_id = param_id;
1414 cmd_set_param.hdr.token = 0;
1415
1416 rc = apr_send_pkt(usc->apr, (uint32_t *) &cmd_set_param);
1417
1418 if (rc < 0) {
1419 pr_err("%s:write op[0x%x];rc[%d]\n",
1420 __func__, cmd_set_param.hdr.opcode, rc);
1421 }
1422
1423 rc = wait_event_timeout(usc->cmd_wait,
1424 (atomic_read(&usc->cmd_state) == 0),
1425 Q6USM_TIMEOUT_JIFFIES);
1426 if (!rc) {
1427 rc = -ETIME;
1428 pr_err("%s: CMD_SET_PARAM: timeout=%d\n",
1429 __func__, Q6USM_TIMEOUT_JIFFIES);
1430 } else
1431 rc = 0;
1432
1433 return rc;
1434}
1435
1436int q6usm_get_us_stream_param(int dir, struct us_client *usc,
1437 uint32_t module_id, uint32_t param_id, uint32_t buf_size)
1438{
1439 int rc = 0;
1440 struct usm_stream_cmd_get_param cmd_get_param;
1441 struct us_port_data *port = NULL;
1442
1443 if ((usc == NULL) || (usc->apr == NULL)) {
1444 pr_err("%s: APR handle NULL\n", __func__);
1445 return -EINVAL;
1446 }
1447 port = &usc->port[dir];
1448
1449 q6usm_add_hdr(usc, &cmd_get_param.hdr, sizeof(cmd_get_param), true);
1450
1451 cmd_get_param.hdr.opcode = USM_STREAM_CMD_GET_PARAM;
1452 cmd_get_param.buf_size = buf_size;
1453 cmd_get_param.buf_addr_msw =
1454 msm_audio_populate_upper_32_bits(port->param_phys);
1455 cmd_get_param.buf_addr_lsw = lower_32_bits(port->param_phys);
1456 cmd_get_param.mem_map_handle =
1457 *((uint32_t *)(port->param_buf_mem_handle));
1458 cmd_get_param.module_id = module_id;
1459 cmd_get_param.param_id = param_id;
1460 cmd_get_param.hdr.token = 0;
1461
1462 rc = apr_send_pkt(usc->apr, (uint32_t *) &cmd_get_param);
1463
1464 if (rc < 0) {
1465 pr_err("%s:write op[0x%x];rc[%d]\n",
1466 __func__, cmd_get_param.hdr.opcode, rc);
1467 }
1468
1469 rc = wait_event_timeout(usc->cmd_wait,
1470 (atomic_read(&usc->cmd_state) == 0),
1471 Q6USM_TIMEOUT_JIFFIES);
1472 if (!rc) {
1473 rc = -ETIME;
1474 pr_err("%s: CMD_GET_PARAM: timeout=%d\n",
1475 __func__, Q6USM_TIMEOUT_JIFFIES);
1476 } else
1477 rc = 0;
1478
1479 return rc;
1480}
1481
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301482int __init q6usm_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301483{
1484 pr_debug("%s\n", __func__);
1485 init_waitqueue_head(&this_mmap.cmd_wait);
1486 memset(session, 0, sizeof(session));
1487 return 0;
1488}