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