Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2010-2011, 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 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/uaccess.h> |
| 17 | #include <linux/spinlock.h> |
| 18 | #include <linux/mutex.h> |
| 19 | #include <linux/list.h> |
| 20 | #include <linux/sched.h> |
| 21 | #include <linux/wait.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/fs.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/debugfs.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/sysfs.h> |
| 28 | #include <linux/device.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <mach/peripheral-loader.h> |
| 31 | #include <mach/msm_smd.h> |
| 32 | #include <mach/qdsp6v2/apr.h> |
| 33 | #include <mach/qdsp6v2/apr_tal.h> |
| 34 | #include <mach/qdsp6v2/dsp_debug.h> |
| 35 | #include <mach/subsystem_notif.h> |
| 36 | #include <mach/subsystem_restart.h> |
| 37 | |
| 38 | struct apr_q6 q6; |
| 39 | struct apr_client client[APR_DEST_MAX][APR_CLIENT_MAX]; |
| 40 | static atomic_t dsp_state; |
| 41 | static atomic_t modem_state; |
| 42 | |
| 43 | static wait_queue_head_t dsp_wait; |
| 44 | static wait_queue_head_t modem_wait; |
| 45 | /* Subsystem restart: QDSP6 data, functions */ |
| 46 | static struct workqueue_struct *apr_reset_workqueue; |
| 47 | static void apr_reset_deregister(struct work_struct *work); |
| 48 | struct apr_reset_work { |
| 49 | void *handle; |
| 50 | struct work_struct work; |
| 51 | }; |
| 52 | |
| 53 | |
| 54 | int apr_send_pkt(void *handle, uint32_t *buf) |
| 55 | { |
| 56 | struct apr_svc *svc = handle; |
| 57 | struct apr_client *clnt; |
| 58 | struct apr_hdr *hdr; |
| 59 | uint16_t dest_id; |
| 60 | uint16_t client_id; |
| 61 | uint16_t w_len; |
| 62 | unsigned long flags; |
| 63 | |
| 64 | if (!handle || !buf) { |
| 65 | pr_err("APR: Wrong parameters\n"); |
| 66 | return -EINVAL; |
| 67 | } |
| 68 | if (svc->need_reset) { |
| 69 | pr_err("apr: send_pkt service need reset\n"); |
| 70 | return -ENETRESET; |
| 71 | } |
| 72 | |
| 73 | if ((svc->dest_id == APR_DEST_QDSP6) && |
| 74 | (atomic_read(&dsp_state) == 0)) { |
| 75 | pr_err("apr: Still dsp is not Up\n"); |
| 76 | return -ENETRESET; |
| 77 | } else if ((svc->dest_id == APR_DEST_MODEM) && |
| 78 | (atomic_read(&modem_state) == 0)) { |
| 79 | pr_err("apr: Still Modem is not Up\n"); |
| 80 | return -ENETRESET; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | spin_lock_irqsave(&svc->w_lock, flags); |
| 85 | dest_id = svc->dest_id; |
| 86 | client_id = svc->client_id; |
| 87 | clnt = &client[dest_id][client_id]; |
| 88 | |
| 89 | if (!client[dest_id][client_id].handle) { |
| 90 | pr_err("APR: Still service is not yet opened\n"); |
| 91 | spin_unlock_irqrestore(&svc->w_lock, flags); |
| 92 | return -EINVAL; |
| 93 | } |
| 94 | hdr = (struct apr_hdr *)buf; |
| 95 | |
| 96 | hdr->src_domain = APR_DOMAIN_APPS; |
| 97 | hdr->src_svc = svc->id; |
| 98 | if (dest_id == APR_DEST_MODEM) |
| 99 | hdr->dest_domain = APR_DOMAIN_MODEM; |
| 100 | else if (dest_id == APR_DEST_QDSP6) |
| 101 | hdr->dest_domain = APR_DOMAIN_ADSP; |
| 102 | |
| 103 | hdr->dest_svc = svc->id; |
| 104 | |
| 105 | w_len = apr_tal_write(clnt->handle, buf, hdr->pkt_size); |
| 106 | if (w_len != hdr->pkt_size) |
| 107 | pr_err("Unable to write APR pkt successfully: %d\n", w_len); |
| 108 | spin_unlock_irqrestore(&svc->w_lock, flags); |
| 109 | |
| 110 | return w_len; |
| 111 | } |
| 112 | |
| 113 | static void apr_cb_func(void *buf, int len, void *priv) |
| 114 | { |
| 115 | struct apr_client_data data; |
| 116 | struct apr_client *apr_client; |
| 117 | struct apr_svc *c_svc; |
| 118 | struct apr_hdr *hdr; |
| 119 | uint16_t hdr_size; |
| 120 | uint16_t msg_type; |
| 121 | uint16_t ver; |
| 122 | uint16_t src; |
| 123 | uint16_t svc; |
| 124 | uint16_t clnt; |
| 125 | int i; |
| 126 | int temp_port = 0; |
| 127 | uint32_t *ptr; |
| 128 | |
| 129 | pr_debug("APR2: len = %d\n", len); |
| 130 | ptr = buf; |
| 131 | pr_debug("\n*****************\n"); |
| 132 | for (i = 0; i < len/4; i++) |
| 133 | pr_debug("%x ", ptr[i]); |
| 134 | pr_debug("\n"); |
| 135 | pr_debug("\n*****************\n"); |
| 136 | |
| 137 | if (!buf || len <= APR_HDR_SIZE) { |
| 138 | pr_err("APR: Improper apr pkt received:%p %d\n", |
| 139 | buf, len); |
| 140 | return; |
| 141 | } |
| 142 | hdr = buf; |
| 143 | |
| 144 | ver = hdr->hdr_field; |
| 145 | ver = (ver & 0x000F); |
| 146 | if (ver > APR_PKT_VER + 1) { |
| 147 | pr_err("APR: Wrong version: %d\n", ver); |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | hdr_size = hdr->hdr_field; |
| 152 | hdr_size = ((hdr_size & 0x00F0) >> 0x4) * 4; |
| 153 | if (hdr_size < APR_HDR_SIZE) { |
| 154 | pr_err("APR: Wrong hdr size:%d\n", hdr_size); |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | if (hdr->pkt_size < APR_HDR_SIZE) { |
| 159 | pr_err("APR: Wrong paket size\n"); |
| 160 | return; |
| 161 | } |
| 162 | msg_type = hdr->hdr_field; |
| 163 | msg_type = (msg_type >> 0x08) & 0x0003; |
| 164 | if (msg_type >= APR_MSG_TYPE_MAX && |
| 165 | msg_type != APR_BASIC_RSP_RESULT) { |
| 166 | pr_err("APR: Wrong message type: %d\n", msg_type); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | if (hdr->src_domain >= APR_DOMAIN_MAX || |
| 171 | hdr->dest_domain >= APR_DOMAIN_MAX || |
| 172 | hdr->src_svc >= APR_SVC_MAX || |
| 173 | hdr->dest_svc >= APR_SVC_MAX) { |
| 174 | pr_err("APR: Wrong APR header\n"); |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | svc = hdr->dest_svc; |
| 179 | if (hdr->src_domain == APR_DOMAIN_MODEM) { |
| 180 | src = APR_DEST_MODEM; |
| 181 | if (svc == APR_SVC_MVS || svc == APR_SVC_MVM || |
| 182 | svc == APR_SVC_CVS || svc == APR_SVC_CVP || |
| 183 | svc == APR_SVC_TEST_CLIENT) |
| 184 | clnt = APR_CLIENT_VOICE; |
| 185 | else { |
| 186 | pr_err("APR: Wrong svc :%d\n", svc); |
| 187 | return; |
| 188 | } |
| 189 | } else if (hdr->src_domain == APR_DOMAIN_ADSP) { |
| 190 | src = APR_DEST_QDSP6; |
| 191 | if (svc == APR_SVC_AFE || svc == APR_SVC_ASM || |
| 192 | svc == APR_SVC_VSM || svc == APR_SVC_VPM || |
| 193 | svc == APR_SVC_ADM || svc == APR_SVC_ADSP_CORE || |
| 194 | svc == APR_SVC_TEST_CLIENT || svc == APR_SVC_ADSP_MVM || |
| 195 | svc == APR_SVC_ADSP_CVS || svc == APR_SVC_ADSP_CVP) |
| 196 | clnt = APR_CLIENT_AUDIO; |
| 197 | else { |
| 198 | pr_err("APR: Wrong svc :%d\n", svc); |
| 199 | return; |
| 200 | } |
| 201 | } else { |
| 202 | pr_err("APR: Pkt from wrong source: %d\n", hdr->src_domain); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | pr_debug("src =%d clnt = %d\n", src, clnt); |
| 207 | apr_client = &client[src][clnt]; |
| 208 | for (i = 0; i < APR_SVC_MAX; i++) |
| 209 | if (apr_client->svc[i].id == svc) { |
| 210 | pr_debug("%d\n", apr_client->svc[i].id); |
| 211 | c_svc = &apr_client->svc[i]; |
| 212 | break; |
| 213 | } |
| 214 | |
| 215 | if (i == APR_SVC_MAX) { |
| 216 | pr_err("APR: service is not registered\n"); |
| 217 | return; |
| 218 | } |
| 219 | pr_debug("svc_idx = %d\n", i); |
| 220 | pr_debug("%x %x %x %p %p\n", c_svc->id, c_svc->dest_id, |
| 221 | c_svc->client_id, c_svc->fn, c_svc->priv); |
| 222 | data.payload_size = hdr->pkt_size - hdr_size; |
| 223 | data.opcode = hdr->opcode; |
| 224 | data.src = src; |
| 225 | data.src_port = hdr->src_port; |
| 226 | data.dest_port = hdr->dest_port; |
| 227 | data.token = hdr->token; |
| 228 | data.msg_type = msg_type; |
| 229 | if (data.payload_size > 0) |
| 230 | data.payload = (char *)hdr + hdr_size; |
| 231 | |
| 232 | temp_port = ((data.src_port >> 8) * 8) + (data.src_port & 0xFF); |
| 233 | pr_debug("port = %d t_port = %d\n", data.src_port, temp_port); |
| 234 | if (c_svc->port_cnt && c_svc->port_fn[temp_port]) |
| 235 | c_svc->port_fn[temp_port](&data, c_svc->port_priv[temp_port]); |
| 236 | else if (c_svc->fn) |
| 237 | c_svc->fn(&data, c_svc->priv); |
| 238 | else |
| 239 | pr_err("APR: Rxed a packet for NULL callback\n"); |
| 240 | } |
| 241 | |
| 242 | struct apr_svc *apr_register(char *dest, char *svc_name, apr_fn svc_fn, |
| 243 | uint32_t src_port, void *priv) |
| 244 | { |
| 245 | int client_id = 0; |
| 246 | int svc_idx = 0; |
| 247 | int svc_id = 0; |
| 248 | int dest_id = 0; |
| 249 | int temp_port = 0; |
| 250 | struct apr_svc *svc = NULL; |
| 251 | int rc = 0; |
| 252 | |
| 253 | if (!dest || !svc_name || !svc_fn) |
| 254 | return NULL; |
| 255 | |
| 256 | if (!strcmp(dest, "ADSP")) |
| 257 | dest_id = APR_DEST_QDSP6; |
| 258 | else if (!strcmp(dest, "MODEM")) { |
| 259 | dest_id = APR_DEST_MODEM; |
| 260 | } else { |
| 261 | pr_err("APR: wrong destination\n"); |
| 262 | goto done; |
| 263 | } |
| 264 | |
| 265 | if ((dest_id == APR_DEST_QDSP6) && |
| 266 | (atomic_read(&dsp_state) == 0)) { |
Bharath Ramachandramurthy | e0b5532 | 2011-08-23 10:54:14 -0700 | [diff] [blame^] | 267 | pr_info("%s: Wait for Lpass to bootup\n", __func__); |
| 268 | rc = wait_event_interruptible(dsp_wait, |
| 269 | (atomic_read(&dsp_state) == 1)); |
| 270 | if (rc < 0) { |
| 271 | pr_err("%s: DSP is not Up\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 272 | return NULL; |
| 273 | } |
Bharath Ramachandramurthy | e0b5532 | 2011-08-23 10:54:14 -0700 | [diff] [blame^] | 274 | pr_debug("%s: Lpass Up\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 275 | } else if ((dest_id == APR_DEST_MODEM) && |
| 276 | (atomic_read(&modem_state) == 0)) { |
Bharath Ramachandramurthy | e0b5532 | 2011-08-23 10:54:14 -0700 | [diff] [blame^] | 277 | pr_info("%s: Wait for modem to bootup\n", __func__); |
| 278 | rc = wait_event_interruptible(modem_wait, |
| 279 | (atomic_read(&modem_state) == 1)); |
| 280 | if (rc < 0) { |
| 281 | pr_err("%s: Modem is not Up\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 282 | return NULL; |
| 283 | } |
Bharath Ramachandramurthy | e0b5532 | 2011-08-23 10:54:14 -0700 | [diff] [blame^] | 284 | pr_debug("%s: modem Up\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | if (!strcmp(svc_name, "AFE")) { |
| 288 | client_id = APR_CLIENT_AUDIO; |
| 289 | svc_idx = 0; |
| 290 | svc_id = APR_SVC_AFE; |
| 291 | } else if (!strcmp(svc_name, "ASM")) { |
| 292 | client_id = APR_CLIENT_AUDIO; |
| 293 | svc_idx = 1; |
| 294 | svc_id = APR_SVC_ASM; |
| 295 | } else if (!strcmp(svc_name, "ADM")) { |
| 296 | client_id = APR_CLIENT_AUDIO; |
| 297 | svc_idx = 2; |
| 298 | svc_id = APR_SVC_ADM; |
| 299 | } else if (!strcmp(svc_name, "CORE")) { |
| 300 | client_id = APR_CLIENT_AUDIO; |
| 301 | svc_idx = 3; |
| 302 | svc_id = APR_SVC_ADSP_CORE; |
| 303 | } else if (!strcmp(svc_name, "TEST")) { |
| 304 | if (dest_id == APR_DEST_QDSP6) { |
| 305 | client_id = APR_CLIENT_AUDIO; |
| 306 | svc_idx = 4; |
| 307 | } else { |
| 308 | client_id = APR_CLIENT_VOICE; |
| 309 | svc_idx = 7; |
| 310 | } |
| 311 | svc_id = APR_SVC_TEST_CLIENT; |
| 312 | } else if (!strcmp(svc_name, "VSM")) { |
| 313 | client_id = APR_CLIENT_VOICE; |
| 314 | svc_idx = 0; |
| 315 | svc_id = APR_SVC_VSM; |
| 316 | } else if (!strcmp(svc_name, "VPM")) { |
| 317 | client_id = APR_CLIENT_VOICE; |
| 318 | svc_idx = 1; |
| 319 | svc_id = APR_SVC_VPM; |
| 320 | } else if (!strcmp(svc_name, "MVS")) { |
| 321 | client_id = APR_CLIENT_VOICE; |
| 322 | svc_idx = 2; |
| 323 | svc_id = APR_SVC_MVS; |
| 324 | } else if (!strcmp(svc_name, "MVM")) { |
| 325 | if (dest_id == APR_DEST_MODEM) { |
| 326 | client_id = APR_CLIENT_VOICE; |
| 327 | svc_idx = 3; |
| 328 | svc_id = APR_SVC_MVM; |
| 329 | } else { |
| 330 | client_id = APR_CLIENT_AUDIO; |
| 331 | svc_idx = 5; |
| 332 | svc_id = APR_SVC_ADSP_MVM; |
| 333 | } |
| 334 | } else if (!strcmp(svc_name, "CVS")) { |
| 335 | if (dest_id == APR_DEST_MODEM) { |
| 336 | client_id = APR_CLIENT_VOICE; |
| 337 | svc_idx = 4; |
| 338 | svc_id = APR_SVC_CVS; |
| 339 | } else { |
| 340 | client_id = APR_CLIENT_AUDIO; |
| 341 | svc_idx = 6; |
| 342 | svc_id = APR_SVC_ADSP_CVS; |
| 343 | } |
| 344 | } else if (!strcmp(svc_name, "CVP")) { |
| 345 | if (dest_id == APR_DEST_MODEM) { |
| 346 | client_id = APR_CLIENT_VOICE; |
| 347 | svc_idx = 5; |
| 348 | svc_id = APR_SVC_CVP; |
| 349 | } else { |
| 350 | client_id = APR_CLIENT_AUDIO; |
| 351 | svc_idx = 7; |
| 352 | svc_id = APR_SVC_ADSP_CVP; |
| 353 | } |
| 354 | } else if (!strcmp(svc_name, "SRD")) { |
| 355 | client_id = APR_CLIENT_VOICE; |
| 356 | svc_idx = 6; |
| 357 | svc_id = APR_SVC_SRD; |
| 358 | } else { |
| 359 | pr_err("APR: Wrong svc name\n"); |
| 360 | goto done; |
| 361 | } |
| 362 | |
| 363 | pr_debug("svc name = %s c_id = %d dest_id = %d\n", |
| 364 | svc_name, client_id, dest_id); |
| 365 | mutex_lock(&q6.lock); |
| 366 | if (q6.state == APR_Q6_NOIMG) { |
| 367 | q6.pil = pil_get("q6"); |
| 368 | if (!q6.pil) { |
| 369 | pr_err("APR: Unable to load q6 image\n"); |
| 370 | mutex_unlock(&q6.lock); |
| 371 | return svc; |
| 372 | } |
| 373 | q6.state = APR_Q6_LOADED; |
| 374 | } |
| 375 | mutex_unlock(&q6.lock); |
| 376 | mutex_lock(&client[dest_id][client_id].m_lock); |
| 377 | if (!client[dest_id][client_id].handle) { |
| 378 | client[dest_id][client_id].handle = apr_tal_open(client_id, |
| 379 | dest_id, APR_DL_SMD, apr_cb_func, NULL); |
| 380 | if (!client[dest_id][client_id].handle) { |
| 381 | svc = NULL; |
| 382 | pr_err("APR: Unable to open handle\n"); |
| 383 | mutex_unlock(&client[dest_id][client_id].m_lock); |
| 384 | goto done; |
| 385 | } |
| 386 | } |
| 387 | mutex_unlock(&client[dest_id][client_id].m_lock); |
| 388 | svc = &client[dest_id][client_id].svc[svc_idx]; |
| 389 | mutex_lock(&svc->m_lock); |
| 390 | client[dest_id][client_id].id = client_id; |
| 391 | if (svc->need_reset) { |
| 392 | mutex_unlock(&svc->m_lock); |
| 393 | pr_err("APR: Service needs reset\n"); |
| 394 | goto done; |
| 395 | } |
| 396 | svc->priv = priv; |
| 397 | svc->id = svc_id; |
| 398 | svc->dest_id = dest_id; |
| 399 | svc->client_id = client_id; |
| 400 | if (src_port != 0xFFFFFFFF) { |
| 401 | temp_port = ((src_port >> 8) * 8) + (src_port & 0xFF); |
| 402 | pr_debug("port = %d t_port = %d\n", src_port, temp_port); |
Vasudeva Rao Thumati | 86edf6c | 2011-07-06 16:25:13 +0530 | [diff] [blame] | 403 | if (temp_port >= APR_MAX_PORTS || temp_port < 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 404 | pr_err("APR: temp_port out of bounds\n"); |
| 405 | mutex_unlock(&svc->m_lock); |
| 406 | return NULL; |
| 407 | } |
| 408 | if (!svc->port_cnt && !svc->svc_cnt) |
| 409 | client[dest_id][client_id].svc_cnt++; |
| 410 | svc->port_cnt++; |
| 411 | svc->port_fn[temp_port] = svc_fn; |
| 412 | svc->port_priv[temp_port] = priv; |
| 413 | } else { |
| 414 | if (!svc->fn) { |
| 415 | if (!svc->port_cnt && !svc->svc_cnt) |
| 416 | client[dest_id][client_id].svc_cnt++; |
| 417 | svc->fn = svc_fn; |
| 418 | if (svc->port_cnt) |
| 419 | svc->svc_cnt++; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | mutex_unlock(&svc->m_lock); |
| 424 | done: |
| 425 | return svc; |
| 426 | } |
| 427 | |
| 428 | static void apr_reset_deregister(struct work_struct *work) |
| 429 | { |
| 430 | struct apr_svc *handle = NULL; |
| 431 | struct apr_reset_work *apr_reset = |
| 432 | container_of(work, struct apr_reset_work, work); |
| 433 | |
| 434 | handle = apr_reset->handle; |
| 435 | pr_debug("%s:handle[%p]\n", __func__, handle); |
| 436 | apr_deregister(handle); |
| 437 | kfree(apr_reset); |
| 438 | } |
| 439 | |
| 440 | int apr_deregister(void *handle) |
| 441 | { |
| 442 | struct apr_svc *svc = handle; |
| 443 | struct apr_client *clnt; |
| 444 | uint16_t dest_id; |
| 445 | uint16_t client_id; |
| 446 | |
| 447 | if (!handle) |
| 448 | return -EINVAL; |
| 449 | |
| 450 | mutex_lock(&svc->m_lock); |
| 451 | dest_id = svc->dest_id; |
| 452 | client_id = svc->client_id; |
| 453 | clnt = &client[dest_id][client_id]; |
| 454 | |
| 455 | if (svc->port_cnt > 0 || svc->svc_cnt > 0) { |
| 456 | if (svc->port_cnt) |
| 457 | svc->port_cnt--; |
| 458 | else if (svc->svc_cnt) |
| 459 | svc->svc_cnt--; |
| 460 | if (!svc->port_cnt && !svc->svc_cnt) { |
| 461 | client[dest_id][client_id].svc_cnt--; |
| 462 | svc->need_reset = 0x0; |
| 463 | } |
| 464 | } else if (client[dest_id][client_id].svc_cnt > 0) { |
| 465 | client[dest_id][client_id].svc_cnt--; |
| 466 | if (!client[dest_id][client_id].svc_cnt) { |
| 467 | svc->need_reset = 0x0; |
| 468 | pr_debug("%s: service is reset %p\n", __func__, svc); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | if (!svc->port_cnt && !svc->svc_cnt) { |
| 473 | svc->priv = NULL; |
| 474 | svc->id = 0; |
| 475 | svc->fn = NULL; |
| 476 | svc->dest_id = 0; |
| 477 | svc->client_id = 0; |
| 478 | svc->need_reset = 0x0; |
| 479 | } |
| 480 | if (client[dest_id][client_id].handle && |
| 481 | !client[dest_id][client_id].svc_cnt) { |
| 482 | apr_tal_close(client[dest_id][client_id].handle); |
| 483 | client[dest_id][client_id].handle = NULL; |
| 484 | } |
| 485 | mutex_unlock(&svc->m_lock); |
| 486 | |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | void apr_reset(void *handle) |
| 491 | { |
| 492 | struct apr_reset_work *apr_reset_worker = NULL; |
| 493 | |
| 494 | if (!handle) |
| 495 | return; |
| 496 | pr_debug("%s: handle[%p]\n", __func__, handle); |
| 497 | |
| 498 | apr_reset_worker = kzalloc(sizeof(struct apr_reset_work), |
| 499 | GFP_ATOMIC); |
| 500 | if (apr_reset_worker == NULL || apr_reset_workqueue == NULL) { |
| 501 | pr_err("%s: mem failure\n", __func__); |
| 502 | return; |
| 503 | } |
| 504 | apr_reset_worker->handle = handle; |
| 505 | INIT_WORK(&apr_reset_worker->work, apr_reset_deregister); |
| 506 | queue_work(apr_reset_workqueue, &apr_reset_worker->work); |
| 507 | } |
| 508 | |
| 509 | void change_q6_state(int state) |
| 510 | { |
| 511 | mutex_lock(&q6.lock); |
| 512 | q6.state = state; |
| 513 | mutex_unlock(&q6.lock); |
| 514 | } |
| 515 | |
| 516 | int adsp_state(int state) |
| 517 | { |
| 518 | pr_info("dsp state = %d\n", state); |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | /* Dispatch the Reset events to Modem and audio clients */ |
| 523 | void dispatch_event(unsigned long code, unsigned short proc) |
| 524 | { |
| 525 | struct apr_client *apr_client; |
| 526 | struct apr_client_data data; |
| 527 | struct apr_svc *svc; |
| 528 | uint16_t clnt; |
| 529 | int i, j; |
| 530 | |
| 531 | data.opcode = RESET_EVENTS; |
| 532 | data.reset_event = code; |
| 533 | data.reset_proc = proc; |
| 534 | |
| 535 | clnt = APR_CLIENT_AUDIO; |
| 536 | apr_client = &client[proc][clnt]; |
| 537 | for (i = 0; i < APR_SVC_MAX; i++) { |
| 538 | mutex_lock(&apr_client->svc[i].m_lock); |
| 539 | if (apr_client->svc[i].fn) { |
| 540 | apr_client->svc[i].need_reset = 0x1; |
| 541 | apr_client->svc[i].fn(&data, apr_client->svc[i].priv); |
| 542 | } |
| 543 | if (apr_client->svc[i].port_cnt) { |
| 544 | svc = &(apr_client->svc[i]); |
| 545 | svc->need_reset = 0x1; |
| 546 | for (j = 0; j < APR_MAX_PORTS; j++) |
| 547 | if (svc->port_fn[j]) |
| 548 | svc->port_fn[j](&data, |
| 549 | svc->port_priv[j]); |
| 550 | } |
| 551 | mutex_unlock(&apr_client->svc[i].m_lock); |
| 552 | } |
| 553 | |
| 554 | clnt = APR_CLIENT_VOICE; |
| 555 | apr_client = &client[proc][clnt]; |
| 556 | for (i = 0; i < APR_SVC_MAX; i++) { |
| 557 | mutex_lock(&apr_client->svc[i].m_lock); |
| 558 | if (apr_client->svc[i].fn) { |
| 559 | apr_client->svc[i].need_reset = 0x1; |
| 560 | apr_client->svc[i].fn(&data, apr_client->svc[i].priv); |
| 561 | } |
| 562 | if (apr_client->svc[i].port_cnt) { |
| 563 | svc = &(apr_client->svc[i]); |
| 564 | svc->need_reset = 0x1; |
| 565 | for (j = 0; j < APR_MAX_PORTS; j++) |
| 566 | if (svc->port_fn[j]) |
| 567 | svc->port_fn[j](&data, |
| 568 | svc->port_priv[j]); |
| 569 | } |
| 570 | mutex_unlock(&apr_client->svc[i].m_lock); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | static int modem_notifier_cb(struct notifier_block *this, unsigned long code, |
| 575 | void *_cmd) |
| 576 | { |
| 577 | switch (code) { |
| 578 | case SUBSYS_BEFORE_SHUTDOWN: |
| 579 | pr_debug("M-Notify: Shutdown started\n"); |
| 580 | atomic_set(&modem_state, 0); |
| 581 | dispatch_event(code, APR_DEST_MODEM); |
| 582 | break; |
| 583 | case SUBSYS_AFTER_SHUTDOWN: |
| 584 | pr_debug("M-Notify: Shutdown Completed\n"); |
| 585 | break; |
| 586 | case SUBSYS_BEFORE_POWERUP: |
| 587 | pr_debug("M-notify: Bootup started\n"); |
| 588 | break; |
| 589 | case SUBSYS_AFTER_POWERUP: |
| 590 | if (atomic_read(&modem_state) == 0) { |
| 591 | atomic_set(&modem_state, 1); |
| 592 | wake_up(&modem_wait); |
| 593 | } |
| 594 | pr_debug("M-Notify: Bootup Completed\n"); |
| 595 | break; |
| 596 | default: |
| 597 | pr_err("M-Notify: General: %lu\n", code); |
| 598 | break; |
| 599 | } |
| 600 | return NOTIFY_DONE; |
| 601 | } |
| 602 | |
| 603 | static struct notifier_block mnb = { |
| 604 | .notifier_call = modem_notifier_cb, |
| 605 | }; |
| 606 | |
| 607 | static int lpass_notifier_cb(struct notifier_block *this, unsigned long code, |
| 608 | void *_cmd) |
| 609 | { |
| 610 | switch (code) { |
| 611 | case SUBSYS_BEFORE_SHUTDOWN: |
| 612 | pr_debug("L-Notify: Shutdown started\n"); |
| 613 | atomic_set(&dsp_state, 0); |
| 614 | dispatch_event(code, APR_DEST_QDSP6); |
| 615 | break; |
| 616 | case SUBSYS_AFTER_SHUTDOWN: |
| 617 | pr_debug("L-Notify: Shutdown Completed\n"); |
| 618 | break; |
| 619 | case SUBSYS_BEFORE_POWERUP: |
| 620 | pr_debug("L-notify: Bootup started\n"); |
| 621 | break; |
| 622 | case SUBSYS_AFTER_POWERUP: |
| 623 | if (atomic_read(&dsp_state) == 0) { |
| 624 | atomic_set(&dsp_state, 1); |
| 625 | wake_up(&dsp_wait); |
| 626 | } |
| 627 | pr_debug("L-Notify: Bootup Completed\n"); |
| 628 | break; |
| 629 | default: |
| 630 | pr_err("L-Notify: Generel: %lu\n", code); |
| 631 | break; |
| 632 | } |
| 633 | return NOTIFY_DONE; |
| 634 | } |
| 635 | |
| 636 | static struct notifier_block lnb = { |
| 637 | .notifier_call = lpass_notifier_cb, |
| 638 | }; |
| 639 | |
| 640 | |
| 641 | static int __init apr_init(void) |
| 642 | { |
| 643 | int i, j, k; |
| 644 | |
| 645 | for (i = 0; i < APR_DEST_MAX; i++) |
| 646 | for (j = 0; j < APR_CLIENT_MAX; j++) { |
| 647 | mutex_init(&client[i][j].m_lock); |
| 648 | for (k = 0; k < APR_SVC_MAX; k++) { |
| 649 | mutex_init(&client[i][j].svc[k].m_lock); |
| 650 | spin_lock_init(&client[i][j].svc[k].w_lock); |
| 651 | } |
| 652 | } |
| 653 | mutex_init(&q6.lock); |
| 654 | dsp_debug_register(adsp_state); |
| 655 | apr_reset_workqueue = |
| 656 | create_singlethread_workqueue("apr_driver"); |
| 657 | if (!apr_reset_workqueue) |
| 658 | return -ENOMEM; |
| 659 | return 0; |
| 660 | } |
| 661 | device_initcall(apr_init); |
| 662 | |
| 663 | static int __init apr_late_init(void) |
| 664 | { |
| 665 | void *ret; |
| 666 | init_waitqueue_head(&dsp_wait); |
| 667 | init_waitqueue_head(&modem_wait); |
| 668 | atomic_set(&dsp_state, 1); |
| 669 | atomic_set(&modem_state, 1); |
| 670 | ret = subsys_notif_register_notifier("modem", &mnb); |
| 671 | pr_debug("subsys_register_notifier: ret1 = %p\n", ret); |
| 672 | ret = subsys_notif_register_notifier("lpass", &lnb); |
| 673 | pr_debug("subsys_register_notifier: ret2 = %p\n", ret); |
| 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | late_initcall(apr_late_init); |