blob: 2d0607cd36a532032807626e8b64be9752b6a4e5 [file] [log] [blame]
Harmandeep Singhe5ddfe32012-05-26 09:39:25 -07001/* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
2 *
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
14#include <linux/fs.h>
15#include <linux/module.h>
16#include <linux/miscdevice.h>
17#include <linux/slab.h>
18#include <linux/uaccess.h>
19#include <linux/mutex.h>
20#include <linux/sched.h>
21#include <linux/msm_audio_acdb.h>
22#include <asm/atomic.h>
23#include <mach/qdsp6v2/audio_acdb.h>
24#include <mach/qdsp6v2/rtac.h>
25#include "q6audio_common.h"
26#include <sound/q6afe-v2.h>
27
28#ifndef CONFIG_RTAC
29
30void rtac_add_adm_device(u32 port_id, u32 copp_id, u32 path_id, u32 popp_id) {}
31void rtac_remove_adm_device(u32 port_id) {}
32void rtac_remove_popp_from_adm_devices(u32 popp_id) {}
33void rtac_set_adm_handle(void *handle) {}
34bool rtac_make_adm_callback(uint32_t *payload, u32 payload_size)
35 {return false; }
36void rtac_set_asm_handle(u32 session_id, void *handle) {}
37bool rtac_make_asm_callback(u32 session_id, uint32_t *payload,
38 u32 payload_size) {return false; }
39void rtac_add_voice(u32 cvs_handle, u32 cvp_handle, u32 rx_afe_port,
40 u32 tx_afe_port, u32 session_id) {}
41void rtac_remove_voice(u32 cvs_handle) {}
42void rtac_set_voice_handle(u32 mode, void *handle) {}
43bool rtac_make_voice_callback(u32 mode, uint32_t *payload,
44 u32 payload_size) {return false; }
45
46#else
47
48#define VOICE_CMD_SET_PARAM 0x00011006
49#define VOICE_CMD_GET_PARAM 0x00011007
50#define VOICE_EVT_GET_PARAM_ACK 0x00011008
51
52/* Max size of payload (buf size - apr header) */
53#define MAX_PAYLOAD_SIZE 4076
54#define RTAC_MAX_ACTIVE_DEVICES 4
55#define RTAC_MAX_ACTIVE_VOICE_COMBOS 2
56#define RTAC_MAX_ACTIVE_POPP 8
57#define RTAC_BUF_SIZE 4096
58
59#define TIMEOUT_MS 1000
60
61/* APR data */
62struct rtac_apr_data {
63 void *apr_handle;
64 atomic_t cmd_state;
65 wait_queue_head_t cmd_wait;
66};
67
68static struct rtac_apr_data rtac_adm_apr_data;
69static struct rtac_apr_data rtac_asm_apr_data[SESSION_MAX+1];
70static struct rtac_apr_data rtac_voice_apr_data[RTAC_VOICE_MODES];
71
72
73/* ADM info & APR */
74struct rtac_adm_data {
75 uint32_t topology_id;
76 uint32_t afe_port;
77 uint32_t copp;
78 uint32_t num_of_popp;
79 uint32_t popp[RTAC_MAX_ACTIVE_POPP];
80};
81
82struct rtac_adm {
83 uint32_t num_of_dev;
84 struct rtac_adm_data device[RTAC_MAX_ACTIVE_DEVICES];
85};
86static struct rtac_adm rtac_adm_data;
87static u32 rtac_adm_payload_size;
88static u32 rtac_adm_user_buf_size;
89static u8 *rtac_adm_buffer;
90
91
92/* ASM APR */
93static u32 rtac_asm_payload_size;
94static u32 rtac_asm_user_buf_size;
95static u8 *rtac_asm_buffer;
96
97
98/* Voice info & APR */
99struct rtac_voice_data {
100 uint32_t tx_topology_id;
101 uint32_t rx_topology_id;
102 uint32_t tx_afe_port;
103 uint32_t rx_afe_port;
104 uint16_t cvs_handle;
105 uint16_t cvp_handle;
106};
107
108struct rtac_voice {
109 uint32_t num_of_voice_combos;
110 struct rtac_voice_data voice[RTAC_MAX_ACTIVE_VOICE_COMBOS];
111};
112
113static struct rtac_voice rtac_voice_data;
114static u32 rtac_voice_payload_size;
115static u32 rtac_voice_user_buf_size;
116static u8 *rtac_voice_buffer;
117static u32 voice_session_id[RTAC_MAX_ACTIVE_VOICE_COMBOS];
118
119
120struct mutex rtac_adm_mutex;
121struct mutex rtac_adm_apr_mutex;
122struct mutex rtac_asm_apr_mutex;
123struct mutex rtac_voice_mutex;
124struct mutex rtac_voice_apr_mutex;
125
126static int rtac_open(struct inode *inode, struct file *f)
127{
128 pr_debug("%s\n", __func__);
129 return 0;
130}
131
132static int rtac_release(struct inode *inode, struct file *f)
133{
134 pr_debug("%s\n", __func__);
135 return 0;
136}
137
138/* ADM Info */
139void add_popp(u32 dev_idx, u32 port_id, u32 popp_id)
140{
141 u32 i = 0;
142
143 for (; i < rtac_adm_data.device[dev_idx].num_of_popp; i++)
144 if (rtac_adm_data.device[dev_idx].popp[i] == popp_id)
145 goto done;
146
147 if (rtac_adm_data.device[dev_idx].num_of_popp ==
148 RTAC_MAX_ACTIVE_POPP) {
149 pr_err("%s, Max POPP!\n", __func__);
150 goto done;
151 }
152 rtac_adm_data.device[dev_idx].popp[
153 rtac_adm_data.device[dev_idx].num_of_popp++] = popp_id;
154done:
155 return;
156}
157
158void rtac_add_adm_device(u32 port_id, u32 copp_id, u32 path_id, u32 popp_id)
159{
160 u32 i = 0;
161 pr_debug("%s: port_id = %d, popp_id = %d\n", __func__, port_id,
162 popp_id);
163
164 mutex_lock(&rtac_adm_mutex);
165 if (rtac_adm_data.num_of_dev == RTAC_MAX_ACTIVE_DEVICES) {
166 pr_err("%s, Can't add anymore RTAC devices!\n", __func__);
167 goto done;
168 }
169
170 /* Check if device already added */
171 if (rtac_adm_data.num_of_dev != 0) {
172 for (; i < rtac_adm_data.num_of_dev; i++) {
173 if (rtac_adm_data.device[i].afe_port == port_id) {
174 add_popp(i, port_id, popp_id);
175 goto done;
176 }
177 if (rtac_adm_data.device[i].num_of_popp ==
178 RTAC_MAX_ACTIVE_POPP) {
179 pr_err("%s, Max POPP!\n", __func__);
180 goto done;
181 }
182 }
183 }
184
185 /* Add device */
186 rtac_adm_data.num_of_dev++;
187
188 if (path_id == ADM_PATH_PLAYBACK)
189 rtac_adm_data.device[i].topology_id =
190 get_adm_rx_topology();
191 else
192 rtac_adm_data.device[i].topology_id =
193 get_adm_tx_topology();
194 rtac_adm_data.device[i].afe_port = port_id;
195 rtac_adm_data.device[i].copp = copp_id;
196 rtac_adm_data.device[i].popp[
197 rtac_adm_data.device[i].num_of_popp++] = popp_id;
198done:
199 mutex_unlock(&rtac_adm_mutex);
200 return;
201}
202
203static void shift_adm_devices(u32 dev_idx)
204{
205 for (; dev_idx < rtac_adm_data.num_of_dev; dev_idx++) {
206 memcpy(&rtac_adm_data.device[dev_idx],
207 &rtac_adm_data.device[dev_idx + 1],
208 sizeof(rtac_adm_data.device[dev_idx]));
209 memset(&rtac_adm_data.device[dev_idx + 1], 0,
210 sizeof(rtac_adm_data.device[dev_idx]));
211 }
212}
213
214static void shift_popp(u32 copp_idx, u32 popp_idx)
215{
216 for (; popp_idx < rtac_adm_data.device[copp_idx].num_of_popp;
217 popp_idx++) {
218 memcpy(&rtac_adm_data.device[copp_idx].popp[popp_idx],
219 &rtac_adm_data.device[copp_idx].popp[popp_idx + 1],
220 sizeof(uint32_t));
221 memset(&rtac_adm_data.device[copp_idx].popp[popp_idx + 1], 0,
222 sizeof(uint32_t));
223 }
224}
225
226void rtac_remove_adm_device(u32 port_id)
227{
228 s32 i;
229 pr_debug("%s: port_id = %d\n", __func__, port_id);
230
231 mutex_lock(&rtac_adm_mutex);
232 /* look for device */
233 for (i = 0; i < rtac_adm_data.num_of_dev; i++) {
234 if (rtac_adm_data.device[i].afe_port == port_id) {
235 memset(&rtac_adm_data.device[i], 0,
236 sizeof(rtac_adm_data.device[i]));
237 rtac_adm_data.num_of_dev--;
238
239 if (rtac_adm_data.num_of_dev >= 1) {
240 shift_adm_devices(i);
241 break;
242 }
243 }
244 }
245
246 mutex_unlock(&rtac_adm_mutex);
247 return;
248}
249
250void rtac_remove_popp_from_adm_devices(u32 popp_id)
251{
252 s32 i, j;
253 pr_debug("%s: popp_id = %d\n", __func__, popp_id);
254
255 mutex_lock(&rtac_adm_mutex);
256
257 for (i = 0; i < rtac_adm_data.num_of_dev; i++) {
258 for (j = 0; j < rtac_adm_data.device[i].num_of_popp; j++) {
259 if (rtac_adm_data.device[i].popp[j] == popp_id) {
260 rtac_adm_data.device[i].popp[j] = 0;
261 rtac_adm_data.device[i].num_of_popp--;
262 shift_popp(i, j);
263 }
264 }
265 }
266
267 mutex_unlock(&rtac_adm_mutex);
268}
269
270/* Voice Info */
271static void set_rtac_voice_data(int idx, u32 cvs_handle, u32 cvp_handle,
272 u32 rx_afe_port, u32 tx_afe_port,
273 u32 session_id)
274{
275 rtac_voice_data.voice[idx].tx_topology_id = get_voice_tx_topology();
276 rtac_voice_data.voice[idx].rx_topology_id = get_voice_rx_topology();
277 rtac_voice_data.voice[idx].tx_afe_port = tx_afe_port;
278 rtac_voice_data.voice[idx].rx_afe_port = rx_afe_port;
279 rtac_voice_data.voice[idx].cvs_handle = cvs_handle;
280 rtac_voice_data.voice[idx].cvp_handle = cvp_handle;
281
282 /* Store session ID for voice RTAC */
283 voice_session_id[idx] = session_id;
284}
285
286void rtac_add_voice(u32 cvs_handle, u32 cvp_handle, u32 rx_afe_port,
287 u32 tx_afe_port, u32 session_id)
288{
289 u32 i = 0;
290 pr_debug("%s\n", __func__);
291 mutex_lock(&rtac_voice_mutex);
292
293 if (rtac_voice_data.num_of_voice_combos ==
294 RTAC_MAX_ACTIVE_VOICE_COMBOS) {
295 pr_err("%s, Can't add anymore RTAC devices!\n", __func__);
296 goto done;
297 }
298
299 /* Check if device already added */
300 if (rtac_voice_data.num_of_voice_combos != 0) {
301 for (; i < rtac_voice_data.num_of_voice_combos; i++) {
302 if (rtac_voice_data.voice[i].cvs_handle ==
303 cvs_handle) {
304 set_rtac_voice_data(i, cvs_handle, cvp_handle,
305 rx_afe_port, tx_afe_port,
306 session_id);
307 goto done;
308 }
309 }
310 }
311
312 /* Add device */
313 rtac_voice_data.num_of_voice_combos++;
314 set_rtac_voice_data(i, cvs_handle, cvp_handle,
315 rx_afe_port, tx_afe_port,
316 session_id);
317done:
318 mutex_unlock(&rtac_voice_mutex);
319 return;
320}
321
322static void shift_voice_devices(u32 idx)
323{
324 for (; idx < rtac_voice_data.num_of_voice_combos - 1; idx++) {
325 memcpy(&rtac_voice_data.voice[idx],
326 &rtac_voice_data.voice[idx + 1],
327 sizeof(rtac_voice_data.voice[idx]));
328 voice_session_id[idx] = voice_session_id[idx + 1];
329 }
330}
331
332void rtac_remove_voice(u32 cvs_handle)
333{
334 u32 i = 0;
335 pr_debug("%s\n", __func__);
336
337 mutex_lock(&rtac_voice_mutex);
338 /* look for device */
339 for (i = 0; i < rtac_voice_data.num_of_voice_combos; i++) {
340 if (rtac_voice_data.voice[i].cvs_handle == cvs_handle) {
341 shift_voice_devices(i);
342 rtac_voice_data.num_of_voice_combos--;
343 memset(&rtac_voice_data.voice[
344 rtac_voice_data.num_of_voice_combos], 0,
345 sizeof(rtac_voice_data.voice
346 [rtac_voice_data.num_of_voice_combos]));
347 voice_session_id[rtac_voice_data.num_of_voice_combos]
348 = 0;
349 break;
350 }
351 }
352 mutex_unlock(&rtac_voice_mutex);
353 return;
354}
355
356static int get_voice_index(u32 cvs_handle)
357{
358 u32 i;
359
360 for (i = 0; i < rtac_voice_data.num_of_voice_combos; i++) {
361 if (rtac_voice_data.voice[i].cvs_handle == cvs_handle)
362 return i;
363 }
364
365 pr_err("%s: No voice index for CVS handle %d found returning 0\n",
366 __func__, cvs_handle);
367 return 0;
368}
369
370
371/* ADM APR */
372void rtac_set_adm_handle(void *handle)
373{
374 pr_debug("%s: handle = %d\n", __func__, (unsigned int)handle);
375
376 mutex_lock(&rtac_adm_apr_mutex);
377 rtac_adm_apr_data.apr_handle = handle;
378 mutex_unlock(&rtac_adm_apr_mutex);
379}
380
381bool rtac_make_adm_callback(uint32_t *payload, u32 payload_size)
382{
383 pr_debug("%s:cmd_state = %d\n", __func__,
384 atomic_read(&rtac_adm_apr_data.cmd_state));
385 if (atomic_read(&rtac_adm_apr_data.cmd_state) != 1)
386 return false;
387
388 /* Offset data for in-band payload */
389 rtac_copy_adm_payload_to_user(payload, payload_size);
390 atomic_set(&rtac_adm_apr_data.cmd_state, 0);
391 wake_up(&rtac_adm_apr_data.cmd_wait);
392 return true;
393}
394
395void rtac_copy_adm_payload_to_user(void *payload, u32 payload_size)
396{
397 pr_debug("%s\n", __func__);
398 rtac_adm_payload_size = payload_size;
399
400 memcpy(rtac_adm_buffer, &payload_size, sizeof(u32));
401 if (payload_size != 0) {
402 if (payload_size > rtac_adm_user_buf_size) {
403 pr_err("%s: Buffer set not big enough for returned data, buf size = %d, ret data = %d\n",
404 __func__, rtac_adm_user_buf_size, payload_size);
405 goto done;
406 }
407 memcpy(rtac_adm_buffer + sizeof(u32), payload, payload_size);
408 }
409done:
410 return;
411}
412
413u32 send_adm_apr(void *buf, u32 opcode)
414{
415 s32 result;
416 u32 count = 0;
417 u32 bytes_returned = 0;
418 u32 port_index = 0;
419 u32 copp_id;
420 u32 payload_size;
421 struct apr_hdr adm_params;
422 pr_debug("%s\n", __func__);
423
424 if (copy_from_user(&count, (void *)buf, sizeof(count))) {
425 pr_err("%s: Copy to user failed! buf = 0x%x\n",
426 __func__, (unsigned int)buf);
427 result = -EFAULT;
428 goto done;
429 }
430
431 if (count <= 0) {
432 pr_err("%s: Invalid buffer size = %d\n", __func__, count);
433 goto done;
434 }
435
436 if (copy_from_user(&payload_size, buf + sizeof(u32), sizeof(u32))) {
437 pr_err("%s: Could not copy payload size from user buffer\n",
438 __func__);
439 goto done;
440 }
441
442
443 if (payload_size > MAX_PAYLOAD_SIZE) {
444 pr_err("%s: Invalid payload size = %d\n",
445 __func__, payload_size);
446 goto done;
447 }
448
449 if (copy_from_user(&copp_id, buf + 2 * sizeof(u32), sizeof(u32))) {
450 pr_err("%s: Could not copy port id from user buffer\n",
451 __func__);
452 goto done;
453 }
454
455 for (port_index = 0; port_index < AFE_MAX_PORTS; port_index++) {
456 if (adm_get_copp_id(port_index) == copp_id)
457 break;
458 }
459 if (port_index >= AFE_MAX_PORTS) {
460 pr_err("%s: Could not find port index for copp = %d\n",
461 __func__, copp_id);
462 goto done;
463 }
464
465 mutex_lock(&rtac_adm_apr_mutex);
466 if (rtac_adm_apr_data.apr_handle == NULL) {
467 pr_err("%s: APR not initialized\n", __func__);
468 goto err;
469 }
470
471 /* Set globals for copy of returned payload */
472 rtac_adm_user_buf_size = count;
473 /* Copy buffer to in-band payload */
474 if (copy_from_user(rtac_adm_buffer + sizeof(adm_params),
475 buf + 3 * sizeof(u32), payload_size)) {
476 pr_err("%s: Could not copy payload from user buffer\n",
477 __func__);
478 goto err;
479 }
480
481 /* Pack header */
482 adm_params.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
483 APR_HDR_LEN(20), APR_PKT_VER);
484 adm_params.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
485 payload_size);
486 adm_params.src_svc = APR_SVC_ADM;
487 adm_params.src_domain = APR_DOMAIN_APPS;
488 adm_params.src_port = copp_id;
489 adm_params.dest_svc = APR_SVC_ADM;
490 adm_params.dest_domain = APR_DOMAIN_ADSP;
491 adm_params.dest_port = copp_id;
492 adm_params.token = copp_id;
493 adm_params.opcode = opcode;
494
495 memcpy(rtac_adm_buffer, &adm_params, sizeof(adm_params));
496 atomic_set(&rtac_adm_apr_data.cmd_state, 1);
497
498 pr_debug("%s: Sending RTAC command size = %d\n",
499 __func__, adm_params.pkt_size);
500
501 result = apr_send_pkt(rtac_adm_apr_data.apr_handle,
502 (uint32_t *)rtac_adm_buffer);
503 if (result < 0) {
504 pr_err("%s: Set params failed port = %d, copp = %d\n",
505 __func__, port_index, copp_id);
506 goto err;
507 }
508 /* Wait for the callback */
509 result = wait_event_timeout(rtac_adm_apr_data.cmd_wait,
510 (atomic_read(&rtac_adm_apr_data.cmd_state) == 0),
511 msecs_to_jiffies(TIMEOUT_MS));
512 mutex_unlock(&rtac_adm_apr_mutex);
513 if (!result) {
514 pr_err("%s: Set params timed out port = %d, copp = %d\n",
515 __func__, port_index, copp_id);
516 goto done;
517 }
518
519 if (rtac_adm_payload_size != 0) {
520 if (copy_to_user(buf, rtac_adm_buffer,
521 rtac_adm_payload_size + sizeof(u32))) {
522 pr_err("%s: Could not copy buffer to user,size = %d\n",
523 __func__, payload_size);
524 goto done;
525 }
526 }
527
528 /* Return data written for SET & data read for GET */
529 if (opcode == ADM_CMD_GET_PP_PARAMS_V5)
530 bytes_returned = rtac_adm_payload_size;
531 else
532 bytes_returned = payload_size;
533done:
534 return bytes_returned;
535err:
536 mutex_unlock(&rtac_adm_apr_mutex);
537 return bytes_returned;
538}
539
540
541/* ASM APR */
542void rtac_set_asm_handle(u32 session_id, void *handle)
543{
544 pr_debug("%s\n", __func__);
545
546 mutex_lock(&rtac_asm_apr_mutex);
547 rtac_asm_apr_data[session_id].apr_handle = handle;
548 mutex_unlock(&rtac_asm_apr_mutex);
549}
550
551bool rtac_make_asm_callback(u32 session_id, uint32_t *payload,
552 u32 payload_size)
553{
554 if (atomic_read(&rtac_asm_apr_data[session_id].cmd_state) != 1)
555 return false;
556
557 pr_debug("%s\n", __func__);
558 /* Offset data for in-band payload */
559 rtac_copy_asm_payload_to_user(payload, payload_size);
560 atomic_set(&rtac_asm_apr_data[session_id].cmd_state, 0);
561 wake_up(&rtac_asm_apr_data[session_id].cmd_wait);
562 return true;
563}
564
565void rtac_copy_asm_payload_to_user(void *payload, u32 payload_size)
566{
567 pr_debug("%s\n", __func__);
568 rtac_asm_payload_size = payload_size;
569
570 memcpy(rtac_asm_buffer, &payload_size, sizeof(u32));
571 if (payload_size) {
572 if (payload_size > rtac_asm_user_buf_size) {
573 pr_err("%s: Buffer set not big enough for returned data, buf size = %d, ret data = %d\n",
574 __func__, rtac_asm_user_buf_size, payload_size);
575 goto done;
576 }
577 memcpy(rtac_asm_buffer + sizeof(u32), payload, payload_size);
578 }
579done:
580 return;
581}
582
583u32 send_rtac_asm_apr(void *buf, u32 opcode)
584{
585 s32 result;
586 u32 count = 0;
587 u32 bytes_returned = 0;
588 u32 session_id = 0;
589 u32 payload_size;
590 struct apr_hdr asm_params;
591 pr_debug("%s\n", __func__);
592
593 if (copy_from_user(&count, (void *)buf, sizeof(count))) {
594 pr_err("%s: Copy to user failed! buf = 0x%x\n",
595 __func__, (unsigned int)buf);
596 result = -EFAULT;
597 goto done;
598 }
599
600 if (count <= 0) {
601 pr_err("%s: Invalid buffer size = %d\n", __func__, count);
602 goto done;
603 }
604
605 if (copy_from_user(&payload_size, buf + sizeof(u32), sizeof(u32))) {
606 pr_err("%s: Could not copy payload size from user buffer\n",
607 __func__);
608 goto done;
609 }
610
611 if (payload_size > MAX_PAYLOAD_SIZE) {
612 pr_err("%s: Invalid payload size = %d\n",
613 __func__, payload_size);
614 goto done;
615 }
616
617 if (copy_from_user(&session_id, buf + 2 * sizeof(u32), sizeof(u32))) {
618 pr_err("%s: Could not copy session id from user buffer\n",
619 __func__);
620 goto done;
621 }
622 if (session_id > (SESSION_MAX + 1)) {
623 pr_err("%s: Invalid Session = %d\n", __func__, session_id);
624 goto done;
625 }
626
627 mutex_lock(&rtac_asm_apr_mutex);
628 if (session_id < SESSION_MAX+1) {
629 if (rtac_asm_apr_data[session_id].apr_handle == NULL) {
630 pr_err("%s: APR not initialized\n", __func__);
631 goto err;
632 }
633 }
634
635 /* Set globals for copy of returned payload */
636 rtac_asm_user_buf_size = count;
637
638 /* Copy buffer to in-band payload */
639 if (copy_from_user(rtac_asm_buffer + sizeof(asm_params),
640 buf + 3 * sizeof(u32), payload_size)) {
641 pr_err("%s: Could not copy payload from user buffer\n",
642 __func__);
643 goto err;
644 }
645
646 /* Pack header */
647 asm_params.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
648 APR_HDR_LEN(20), APR_PKT_VER);
649 asm_params.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
650 payload_size);
651 asm_params.src_svc = q6asm_get_apr_service_id(session_id);
652 asm_params.src_domain = APR_DOMAIN_APPS;
653 asm_params.src_port = (session_id << 8) | 0x0001;
654 asm_params.dest_svc = APR_SVC_ASM;
655 asm_params.dest_domain = APR_DOMAIN_ADSP;
656 asm_params.dest_port = (session_id << 8) | 0x0001;
657 asm_params.token = session_id;
658 asm_params.opcode = opcode;
659
660 memcpy(rtac_asm_buffer, &asm_params, sizeof(asm_params));
661 if (session_id < SESSION_MAX+1)
662 atomic_set(&rtac_asm_apr_data[session_id].cmd_state, 1);
663
664 pr_debug("%s: Sending RTAC command size = %d, session_id=%d\n",
665 __func__, asm_params.pkt_size, session_id);
666
667 result = apr_send_pkt(rtac_asm_apr_data[session_id].apr_handle,
668 (uint32_t *)rtac_asm_buffer);
669 if (result < 0) {
670 pr_err("%s: Set params failed session = %d\n",
671 __func__, session_id);
672 goto err;
673 }
674
675 /* Wait for the callback */
676 result = wait_event_timeout(rtac_asm_apr_data[session_id].cmd_wait,
677 (atomic_read(&rtac_asm_apr_data[session_id].cmd_state) == 0),
678 5 * HZ);
679 mutex_unlock(&rtac_asm_apr_mutex);
680 if (!result) {
681 pr_err("%s: Set params timed out session = %d\n",
682 __func__, session_id);
683 goto done;
684 }
685
686 if (rtac_asm_payload_size != 0) {
687 if (copy_to_user(buf, rtac_asm_buffer,
688 rtac_asm_payload_size + sizeof(u32))) {
689 pr_err("%s: Could not copy buffer to user,size = %d\n",
690 __func__, payload_size);
691 goto done;
692 }
693 }
694
695 /* Return data written for SET & data read for GET */
696 if (opcode == ASM_STREAM_CMD_GET_PP_PARAMS_V2)
697 bytes_returned = rtac_asm_payload_size;
698 else
699 bytes_returned = payload_size;
700done:
701 return bytes_returned;
702err:
703 mutex_unlock(&rtac_asm_apr_mutex);
704 return bytes_returned;
705}
706
707
708/* Voice APR */
709void rtac_set_voice_handle(u32 mode, void *handle)
710{
711 pr_debug("%s\n", __func__);
712
713 mutex_lock(&rtac_voice_apr_mutex);
714 rtac_voice_apr_data[mode].apr_handle = handle;
715 mutex_unlock(&rtac_voice_apr_mutex);
716}
717
718bool rtac_make_voice_callback(u32 mode, uint32_t *payload, u32 payload_size)
719{
720 if ((atomic_read(&rtac_voice_apr_data[mode].cmd_state) != 1) ||
721 (mode >= RTAC_VOICE_MODES))
722 return false;
723
724 pr_debug("%s\n", __func__);
725 /* Offset data for in-band payload */
726 rtac_copy_voice_payload_to_user(payload, payload_size);
727 atomic_set(&rtac_voice_apr_data[mode].cmd_state, 0);
728 wake_up(&rtac_voice_apr_data[mode].cmd_wait);
729 return true;
730}
731
732void rtac_copy_voice_payload_to_user(void *payload, u32 payload_size)
733{
734 pr_debug("%s\n", __func__);
735 rtac_voice_payload_size = payload_size;
736
737 memcpy(rtac_voice_buffer, &payload_size, sizeof(u32));
738 if (payload_size) {
739 if (payload_size > rtac_voice_user_buf_size) {
740 pr_err("%s: Buffer set not big enough for returned data, buf size = %d, ret data = %d\n",
741 __func__, rtac_voice_user_buf_size, payload_size);
742 goto done;
743 }
744 memcpy(rtac_voice_buffer + sizeof(u32), payload, payload_size);
745 }
746done:
747 return;
748}
749
750u32 send_voice_apr(u32 mode, void *buf, u32 opcode)
751{
752 s32 result;
753 u32 count = 0;
754 u32 bytes_returned = 0;
755 u32 payload_size;
756 u16 dest_port;
757 struct apr_hdr voice_params;
758 pr_debug("%s\n", __func__);
759
760 if (copy_from_user(&count, (void *)buf, sizeof(count))) {
761 pr_err("%s: Copy to user failed! buf = 0x%x\n",
762 __func__, (unsigned int)buf);
763 result = -EFAULT;
764 goto done;
765 }
766
767 if (count <= 0) {
768 pr_err("%s: Invalid buffer size = %d\n", __func__, count);
769 goto done;
770 }
771
772 if (copy_from_user(&payload_size, buf + sizeof(u32), sizeof(u32))) {
773 pr_err("%s: Could not copy payload size from user buffer\n",
774 __func__);
775 goto done;
776 }
777
778 if (payload_size > MAX_PAYLOAD_SIZE) {
779 pr_err("%s: Invalid payload size = %d\n",
780 __func__, payload_size);
781 goto done;
782 }
783
784 if (copy_from_user(&dest_port, buf + 2 * sizeof(u32), sizeof(u32))) {
785 pr_err("%s: Could not copy port id from user buffer\n",
786 __func__);
787 goto done;
788 }
789
790 if ((mode != RTAC_CVP) && (mode != RTAC_CVS)) {
791 pr_err("%s: Invalid Mode for APR, mode = %d\n",
792 __func__, mode);
793 goto done;
794 }
795
796 mutex_lock(&rtac_voice_apr_mutex);
797 if (rtac_voice_apr_data[mode].apr_handle == NULL) {
798 pr_err("%s: APR not initialized\n", __func__);
799 goto err;
800 }
801
802 /* Set globals for copy of returned payload */
803 rtac_voice_user_buf_size = count;
804
805 /* Copy buffer to in-band payload */
806 if (copy_from_user(rtac_voice_buffer + sizeof(voice_params),
807 buf + 3 * sizeof(u32), payload_size)) {
808 pr_err("%s: Could not copy payload from user buffer\n",
809 __func__);
810 goto err;
811 }
812
813 /* Pack header */
814 voice_params.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
815 APR_HDR_LEN(20), APR_PKT_VER);
816 voice_params.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
817 payload_size);
818 voice_params.src_svc = 0;
819 voice_params.src_domain = APR_DOMAIN_APPS;
820 voice_params.src_port = voice_session_id[
821 get_voice_index(dest_port)];
822 voice_params.dest_svc = 0;
823 voice_params.dest_domain = APR_DOMAIN_MODEM;
824 voice_params.dest_port = dest_port;
825 voice_params.token = 0;
826 voice_params.opcode = opcode;
827
828 memcpy(rtac_voice_buffer, &voice_params, sizeof(voice_params));
829 atomic_set(&rtac_voice_apr_data[mode].cmd_state, 1);
830
831 pr_debug("%s: Sending RTAC command size = %d, opcode = %x\n",
832 __func__, voice_params.pkt_size, opcode);
833
834 result = apr_send_pkt(rtac_voice_apr_data[mode].apr_handle,
835 (uint32_t *)rtac_voice_buffer);
836 if (result < 0) {
837 pr_err("%s: apr_send_pkt failed opcode = %x\n",
838 __func__, opcode);
839 goto err;
840 }
841 /* Wait for the callback */
842 result = wait_event_timeout(rtac_voice_apr_data[mode].cmd_wait,
843 (atomic_read(&rtac_voice_apr_data[mode].cmd_state) == 0),
844 msecs_to_jiffies(TIMEOUT_MS));
845 mutex_unlock(&rtac_voice_apr_mutex);
846 if (!result) {
847 pr_err("%s: apr_send_pkt timed out opcode = %x\n",
848 __func__, opcode);
849 goto done;
850 }
851
852 if (rtac_voice_payload_size != 0) {
853 if (copy_to_user(buf, rtac_voice_buffer,
854 rtac_voice_payload_size + sizeof(u32))) {
855 pr_err("%s: Could not copy buffer to user, size = %d\n",
856 __func__, payload_size);
857 goto done;
858 }
859 }
860
861 /* Return data written for SET & data read for GET */
862 if (opcode == VOICE_CMD_GET_PARAM)
863 bytes_returned = rtac_voice_payload_size;
864 else
865 bytes_returned = payload_size;
866done:
867 return bytes_returned;
868err:
869 mutex_unlock(&rtac_voice_apr_mutex);
870 return bytes_returned;
871}
872
873
874
875static long rtac_ioctl(struct file *f,
876 unsigned int cmd, unsigned long arg)
877{
878 s32 result = 0;
879 pr_debug("%s\n", __func__);
880
881 if (arg == 0) {
882 pr_err("%s: No data sent to driver!\n", __func__);
883 result = -EFAULT;
884 goto done;
885 }
886
887 switch (cmd) {
888 case AUDIO_GET_RTAC_ADM_INFO:
889 if (copy_to_user((void *)arg, &rtac_adm_data,
890 sizeof(rtac_adm_data)))
891 pr_err("%s: Could not copy to userspace!\n", __func__);
892 else
893 result = sizeof(rtac_adm_data);
894 break;
895 case AUDIO_GET_RTAC_VOICE_INFO:
896 if (copy_to_user((void *)arg, &rtac_voice_data,
897 sizeof(rtac_voice_data)))
898 pr_err("%s: Could not copy to userspace!\n", __func__);
899 else
900 result = sizeof(rtac_voice_data);
901 break;
902 case AUDIO_GET_RTAC_ADM_CAL:
903 result = send_adm_apr((void *)arg, ADM_CMD_GET_PP_PARAMS_V5);
904 break;
905 case AUDIO_SET_RTAC_ADM_CAL:
906 result = send_adm_apr((void *)arg, ADM_CMD_SET_PP_PARAMS_V5);
907 break;
908 case AUDIO_GET_RTAC_ASM_CAL:
909 result = send_rtac_asm_apr((void *)arg,
910 ASM_STREAM_CMD_GET_PP_PARAMS_V2);
911 break;
912 case AUDIO_SET_RTAC_ASM_CAL:
913 result = send_rtac_asm_apr((void *)arg,
914 ASM_STREAM_CMD_SET_PP_PARAMS_V2);
915 break;
916 case AUDIO_GET_RTAC_CVS_CAL:
917 result = send_voice_apr(RTAC_CVS, (void *)arg,
918 VOICE_CMD_GET_PARAM);
919 break;
920 case AUDIO_SET_RTAC_CVS_CAL:
921 result = send_voice_apr(RTAC_CVS, (void *)arg,
922 VOICE_CMD_SET_PARAM);
923 break;
924 case AUDIO_GET_RTAC_CVP_CAL:
925 result = send_voice_apr(RTAC_CVP, (void *)arg,
926 VOICE_CMD_GET_PARAM);
927 break;
928 case AUDIO_SET_RTAC_CVP_CAL:
929 result = send_voice_apr(RTAC_CVP, (void *)arg,
930 VOICE_CMD_SET_PARAM);
931 break;
932 default:
933 pr_err("%s: Invalid IOCTL, command = %d!\n",
934 __func__, cmd);
935 }
936done:
937 return result;
938}
939
940
941static const struct file_operations rtac_fops = {
942 .owner = THIS_MODULE,
943 .open = rtac_open,
944 .release = rtac_release,
945 .unlocked_ioctl = rtac_ioctl,
946};
947
948struct miscdevice rtac_misc = {
949 .minor = MISC_DYNAMIC_MINOR,
950 .name = "msm_rtac",
951 .fops = &rtac_fops,
952};
953
954static int __init rtac_init(void)
955{
956 int i = 0;
957 pr_debug("%s\n", __func__);
958
959 /* ADM */
960 memset(&rtac_adm_data, 0, sizeof(rtac_adm_data));
961 rtac_adm_apr_data.apr_handle = NULL;
962 atomic_set(&rtac_adm_apr_data.cmd_state, 0);
963 init_waitqueue_head(&rtac_adm_apr_data.cmd_wait);
964 mutex_init(&rtac_adm_mutex);
965 mutex_init(&rtac_adm_apr_mutex);
966
967 rtac_adm_buffer = kzalloc(RTAC_BUF_SIZE, GFP_KERNEL);
968 if (rtac_adm_buffer == NULL) {
969 pr_err("%s: Could not allocate payload of size = %d\n",
970 __func__, RTAC_BUF_SIZE);
971 goto nomem;
972 }
973
974 /* ASM */
975 for (i = 0; i < SESSION_MAX+1; i++) {
976 rtac_asm_apr_data[i].apr_handle = NULL;
977 atomic_set(&rtac_asm_apr_data[i].cmd_state, 0);
978 init_waitqueue_head(&rtac_asm_apr_data[i].cmd_wait);
979 }
980 mutex_init(&rtac_asm_apr_mutex);
981
982 rtac_asm_buffer = kzalloc(RTAC_BUF_SIZE, GFP_KERNEL);
983 if (rtac_asm_buffer == NULL) {
984 pr_err("%s: Could not allocate payload of size = %d\n",
985 __func__, RTAC_BUF_SIZE);
986 kzfree(rtac_adm_buffer);
987 goto nomem;
988 }
989
990 /* Voice */
991 memset(&rtac_voice_data, 0, sizeof(rtac_voice_data));
992 for (i = 0; i < RTAC_VOICE_MODES; i++) {
993 rtac_voice_apr_data[i].apr_handle = NULL;
994 atomic_set(&rtac_voice_apr_data[i].cmd_state, 0);
995 init_waitqueue_head(&rtac_voice_apr_data[i].cmd_wait);
996 }
997 mutex_init(&rtac_voice_mutex);
998 mutex_init(&rtac_voice_apr_mutex);
999
1000 rtac_voice_buffer = kzalloc(RTAC_BUF_SIZE, GFP_KERNEL);
1001 if (rtac_voice_buffer == NULL) {
1002 pr_err("%s: Could not allocate payload of size = %d\n",
1003 __func__, RTAC_BUF_SIZE);
1004 kzfree(rtac_adm_buffer);
1005 kzfree(rtac_asm_buffer);
1006 goto nomem;
1007 }
1008
1009 return misc_register(&rtac_misc);
1010nomem:
1011 return -ENOMEM;
1012}
1013
1014module_init(rtac_init);
1015
1016MODULE_DESCRIPTION("MSM 8x60 Real-Time Audio Calibration driver");
1017MODULE_LICENSE("GPL v2");
1018
1019#endif