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