Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* low power audio output device |
| 2 | * |
| 3 | * Copyright (C) 2008 Google, Inc. |
| 4 | * Copyright (C) 2008 HTC Corporation |
| 5 | * Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved. |
| 6 | * |
| 7 | * This software is licensed under the terms of the GNU General Public |
| 8 | * License version 2, as published by the Free Software Foundation, and |
| 9 | * may be copied, distributed, and modified under those terms. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/cdev.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/miscdevice.h> |
| 22 | #include <linux/mutex.h> |
| 23 | #include <linux/sched.h> |
| 24 | #include <linux/uaccess.h> |
| 25 | #include <linux/kthread.h> |
| 26 | #include <linux/wait.h> |
| 27 | #include <linux/dma-mapping.h> |
| 28 | #include <linux/debugfs.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/earlysuspend.h> |
| 31 | #include <linux/list.h> |
| 32 | #include <linux/android_pmem.h> |
| 33 | #include <linux/slab.h> |
| 34 | #include <asm/atomic.h> |
| 35 | #include <asm/ioctls.h> |
| 36 | #include <mach/msm_adsp.h> |
| 37 | #include <sound/q6asm.h> |
| 38 | #include <sound/apr_audio.h> |
| 39 | #include "audio_lpa.h" |
| 40 | |
| 41 | #include <linux/msm_audio.h> |
| 42 | #include <linux/wakelock.h> |
| 43 | #include <mach/qdsp6v2/audio_dev_ctl.h> |
| 44 | |
| 45 | #include <mach/debug_mm.h> |
| 46 | #include <linux/fs.h> |
| 47 | |
| 48 | #define MAX_BUF 3 |
| 49 | #define BUFSZ (524288) |
| 50 | |
| 51 | #define AUDDEC_DEC_PCM 0 |
| 52 | |
| 53 | #define AUDLPA_EVENT_NUM 10 /* Default number of pre-allocated event packets */ |
| 54 | |
| 55 | #define __CONTAINS(r, v, l) ({ \ |
| 56 | typeof(r) __r = r; \ |
| 57 | typeof(v) __v = v; \ |
| 58 | typeof(v) __e = __v + l; \ |
| 59 | int res = ((__v >= __r->vaddr) && \ |
| 60 | (__e <= __r->vaddr + __r->len)); \ |
| 61 | res; \ |
| 62 | }) |
| 63 | |
| 64 | #define CONTAINS(r1, r2) ({ \ |
| 65 | typeof(r2) __r2 = r2; \ |
| 66 | __CONTAINS(r1, __r2->vaddr, __r2->len); \ |
| 67 | }) |
| 68 | |
| 69 | #define IN_RANGE(r, v) ({ \ |
| 70 | typeof(r) __r = r; \ |
| 71 | typeof(v) __vv = v; \ |
| 72 | int res = ((__vv >= __r->vaddr) && \ |
| 73 | (__vv < (__r->vaddr + __r->len))); \ |
| 74 | res; \ |
| 75 | }) |
| 76 | |
| 77 | #define OVERLAPS(r1, r2) ({ \ |
| 78 | typeof(r1) __r1 = r1; \ |
| 79 | typeof(r2) __r2 = r2; \ |
| 80 | typeof(__r2->vaddr) __v = __r2->vaddr; \ |
| 81 | typeof(__v) __e = __v + __r2->len - 1; \ |
| 82 | int res = (IN_RANGE(__r1, __v) || IN_RANGE(__r1, __e)); \ |
| 83 | res; \ |
| 84 | }) |
| 85 | |
| 86 | struct audlpa_event { |
| 87 | struct list_head list; |
| 88 | int event_type; |
| 89 | union msm_audio_event_payload payload; |
| 90 | }; |
| 91 | |
| 92 | struct audlpa_pmem_region { |
| 93 | struct list_head list; |
| 94 | struct file *file; |
| 95 | int fd; |
| 96 | void *vaddr; |
| 97 | unsigned long paddr; |
| 98 | unsigned long kvaddr; |
| 99 | unsigned long len; |
| 100 | unsigned ref_cnt; |
| 101 | }; |
| 102 | |
| 103 | struct audlpa_buffer_node { |
| 104 | struct list_head list; |
| 105 | struct msm_audio_aio_buf buf; |
| 106 | unsigned long paddr; |
| 107 | }; |
| 108 | |
| 109 | struct audlpa_dec { |
| 110 | char *name; |
| 111 | int dec_attrb; |
| 112 | long (*ioctl)(struct file *, unsigned int, unsigned long); |
| 113 | int (*set_params)(void *); |
| 114 | }; |
| 115 | |
| 116 | static void audlpa_post_event(struct audio *audio, int type, |
| 117 | union msm_audio_event_payload payload); |
| 118 | static unsigned long audlpa_pmem_fixup(struct audio *audio, void *addr, |
| 119 | unsigned long len, int ref_up); |
| 120 | static void audlpa_async_send_data(struct audio *audio, unsigned needed, |
| 121 | uint32_t token); |
| 122 | static int audlpa_pause(struct audio *audio); |
| 123 | static void audlpa_unmap_pmem_region(struct audio *audio); |
| 124 | static long pcm_ioctl(struct file *file, unsigned int cmd, unsigned long arg); |
| 125 | static int audlpa_set_pcm_params(void *data); |
| 126 | |
| 127 | struct audlpa_dec audlpa_decs[] = { |
| 128 | {"msm_pcm_lp_dec", AUDDEC_DEC_PCM, &pcm_ioctl, |
| 129 | &audlpa_set_pcm_params}, |
| 130 | }; |
| 131 | |
| 132 | static void lpa_listner(u32 evt_id, union auddev_evt_data *evt_payload, |
| 133 | void *private_data) |
| 134 | { |
| 135 | struct audio *audio = (struct audio *) private_data; |
| 136 | int rc = 0; |
| 137 | |
| 138 | switch (evt_id) { |
| 139 | case AUDDEV_EVT_STREAM_VOL_CHG: |
| 140 | audio->volume = evt_payload->session_vol; |
| 141 | pr_debug("%s: AUDDEV_EVT_STREAM_VOL_CHG, stream vol %d, " |
| 142 | "enabled = %d\n", __func__, audio->volume, |
| 143 | audio->out_enabled); |
| 144 | if (audio->out_enabled == 1) { |
| 145 | if (audio->ac) { |
| 146 | rc = q6asm_set_volume(audio->ac, audio->volume); |
| 147 | if (rc < 0) { |
| 148 | pr_err("%s: Send Volume command failed" |
| 149 | " rc=%d\n", __func__, rc); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | break; |
| 154 | default: |
| 155 | pr_err("%s:ERROR:wrong event\n", __func__); |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | static void audlpa_prevent_sleep(struct audio *audio) |
| 161 | { |
| 162 | pr_debug("%s:\n", __func__); |
| 163 | wake_lock(&audio->wakelock); |
| 164 | } |
| 165 | |
| 166 | static void audlpa_allow_sleep(struct audio *audio) |
| 167 | { |
| 168 | pr_debug("%s:\n", __func__); |
| 169 | wake_unlock(&audio->wakelock); |
| 170 | } |
| 171 | |
| 172 | /* must be called with audio->lock held */ |
| 173 | static int audio_enable(struct audio *audio) |
| 174 | { |
| 175 | pr_debug("%s\n", __func__); |
| 176 | |
| 177 | return q6asm_run(audio->ac, 0, 0, 0); |
| 178 | |
| 179 | } |
| 180 | |
| 181 | static void audlpa_async_flush(struct audio *audio) |
| 182 | { |
| 183 | struct audlpa_buffer_node *buf_node; |
| 184 | struct list_head *ptr, *next; |
| 185 | union msm_audio_event_payload payload; |
| 186 | int rc = 0; |
| 187 | |
| 188 | pr_debug("%s:out_enabled = %d, drv_status = 0x%x\n", __func__, |
| 189 | audio->out_enabled, audio->drv_status); |
| 190 | if (audio->out_enabled) { |
| 191 | list_for_each_safe(ptr, next, &audio->out_queue) { |
| 192 | buf_node = list_entry(ptr, struct audlpa_buffer_node, |
| 193 | list); |
| 194 | list_del(&buf_node->list); |
| 195 | payload.aio_buf = buf_node->buf; |
| 196 | audlpa_post_event(audio, AUDIO_EVENT_WRITE_DONE, |
| 197 | payload); |
| 198 | kfree(buf_node); |
| 199 | } |
| 200 | /* Implicitly issue a pause to the decoder before flushing if |
| 201 | it is not in pause state */ |
| 202 | if (!(audio->drv_status & ADRV_STATUS_PAUSE)) { |
| 203 | rc = audlpa_pause(audio); |
| 204 | if (rc < 0) |
| 205 | pr_err("%s: pause cmd failed rc=%d\n", __func__, |
| 206 | rc); |
| 207 | } |
| 208 | |
| 209 | rc = q6asm_cmd(audio->ac, CMD_FLUSH); |
| 210 | if (rc < 0) |
| 211 | pr_err("%s: flush cmd failed rc=%d\n", __func__, rc); |
| 212 | |
| 213 | audio->drv_status &= ~ADRV_STATUS_OBUF_GIVEN; |
| 214 | audio->out_needed = 0; |
| 215 | |
| 216 | if (audio->stopped == 0) { |
| 217 | rc = audio_enable(audio); |
| 218 | if (rc < 0) |
| 219 | pr_err("%s: audio enable failed\n", __func__); |
| 220 | else { |
| 221 | audio->out_enabled = 1; |
| 222 | audio->out_needed = 1; |
| 223 | if (audio->drv_status & ADRV_STATUS_PAUSE) |
| 224 | audio->drv_status &= ~ADRV_STATUS_PAUSE; |
| 225 | } |
| 226 | } |
| 227 | wake_up(&audio->write_wait); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /* must be called with audio->lock held */ |
| 232 | static int audio_disable(struct audio *audio) |
| 233 | { |
| 234 | int rc = 0; |
| 235 | |
| 236 | pr_debug("%s:%d %d\n", __func__, audio->opened, audio->out_enabled); |
| 237 | |
| 238 | if (audio->opened) { |
| 239 | audio->out_enabled = 0; |
| 240 | audio->opened = 0; |
| 241 | rc = q6asm_cmd(audio->ac, CMD_CLOSE); |
| 242 | if (rc < 0) |
| 243 | pr_err("%s: CLOSE cmd failed\n", __func__); |
| 244 | else |
| 245 | pr_debug("%s: rxed CLOSE resp\n", __func__); |
| 246 | audio->drv_status &= ~ADRV_STATUS_OBUF_GIVEN; |
| 247 | wake_up(&audio->write_wait); |
| 248 | audio->out_needed = 0; |
| 249 | } |
| 250 | return rc; |
| 251 | } |
| 252 | static int audlpa_pause(struct audio *audio) |
| 253 | { |
| 254 | int rc = 0; |
| 255 | |
| 256 | pr_debug("%s, enabled = %d\n", __func__, |
| 257 | audio->out_enabled); |
| 258 | if (audio->out_enabled) { |
| 259 | rc = q6asm_cmd(audio->ac, CMD_PAUSE); |
| 260 | if (rc < 0) |
| 261 | pr_err("%s: pause cmd failed rc=%d\n", __func__, rc); |
| 262 | |
| 263 | } else |
| 264 | pr_err("%s: Driver not enabled\n", __func__); |
| 265 | return rc; |
| 266 | } |
| 267 | |
| 268 | /* ------------------- dsp --------------------- */ |
| 269 | static void audlpa_async_send_data(struct audio *audio, unsigned needed, |
| 270 | uint32_t token) |
| 271 | { |
| 272 | unsigned long flags; |
| 273 | struct audio_client *ac; |
| 274 | int rc = 0; |
| 275 | |
| 276 | pr_debug("%s:\n", __func__); |
| 277 | spin_lock_irqsave(&audio->dsp_lock, flags); |
| 278 | |
| 279 | pr_debug("%s: needed = %d, out_needed = %d, token = 0x%x\n", |
| 280 | __func__, needed, audio->out_needed, token); |
| 281 | if (needed && !audio->wflush) { |
| 282 | audio->out_needed = 1; |
| 283 | if (audio->drv_status & ADRV_STATUS_OBUF_GIVEN) { |
| 284 | /* pop one node out of queue */ |
| 285 | union msm_audio_event_payload evt_payload; |
| 286 | struct audlpa_buffer_node *used_buf; |
| 287 | |
| 288 | used_buf = list_first_entry(&audio->out_queue, |
| 289 | struct audlpa_buffer_node, list); |
| 290 | if (token == used_buf->paddr) { |
| 291 | pr_debug("%s, Release: addr: %lx," |
| 292 | " token = 0x%x\n", __func__, |
| 293 | used_buf->paddr, token); |
| 294 | list_del(&used_buf->list); |
| 295 | evt_payload.aio_buf = used_buf->buf; |
| 296 | audlpa_post_event(audio, AUDIO_EVENT_WRITE_DONE, |
| 297 | evt_payload); |
| 298 | kfree(used_buf); |
| 299 | audio->drv_status &= ~ADRV_STATUS_OBUF_GIVEN; |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | pr_debug("%s: out_needed = %d, stopped = %d, drv_status = 0x%x\n", |
| 304 | __func__, audio->out_needed, audio->stopped, |
| 305 | audio->drv_status); |
| 306 | if (audio->out_needed && (audio->stopped == 0)) { |
| 307 | struct audlpa_buffer_node *next_buf; |
| 308 | struct audio_aio_write_param param; |
| 309 | if (!list_empty(&audio->out_queue)) { |
| 310 | pr_debug("%s: list not empty\n", __func__); |
| 311 | next_buf = list_first_entry(&audio->out_queue, |
| 312 | struct audlpa_buffer_node, list); |
| 313 | if (next_buf) { |
| 314 | pr_debug("%s: Send: addr: %lx\n", __func__, |
| 315 | next_buf->paddr); |
| 316 | ac = audio->ac; |
| 317 | param.paddr = next_buf->paddr; |
| 318 | param.len = next_buf->buf.data_len; |
| 319 | param.msw_ts = 0; |
| 320 | param.lsw_ts = 0; |
| 321 | /* No time stamp valid */ |
| 322 | param.flags = NO_TIMESTAMP; |
| 323 | param.uid = next_buf->paddr; |
| 324 | rc = q6asm_async_write(ac, ¶m); |
| 325 | if (rc < 0) |
| 326 | pr_err("%s:q6asm_async_write failed\n", |
| 327 | __func__); |
| 328 | audio->out_needed = 0; |
| 329 | audio->drv_status |= ADRV_STATUS_OBUF_GIVEN; |
| 330 | } |
| 331 | } else if (list_empty(&audio->out_queue) && |
| 332 | (audio->drv_status & ADRV_STATUS_FSYNC)) { |
| 333 | pr_debug("%s: list is empty, reached EOS\n", __func__); |
| 334 | wake_up(&audio->write_wait); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | spin_unlock_irqrestore(&audio->dsp_lock, flags); |
| 339 | } |
| 340 | |
| 341 | static int audlpa_events_pending(struct audio *audio) |
| 342 | { |
| 343 | int empty; |
| 344 | |
| 345 | spin_lock(&audio->event_queue_lock); |
| 346 | empty = !list_empty(&audio->event_queue); |
| 347 | spin_unlock(&audio->event_queue_lock); |
| 348 | return empty || audio->event_abort; |
| 349 | } |
| 350 | |
| 351 | static void audlpa_reset_event_queue(struct audio *audio) |
| 352 | { |
| 353 | struct audlpa_event *drv_evt; |
| 354 | struct list_head *ptr, *next; |
| 355 | |
| 356 | spin_lock(&audio->event_queue_lock); |
| 357 | list_for_each_safe(ptr, next, &audio->event_queue) { |
| 358 | drv_evt = list_first_entry(&audio->event_queue, |
| 359 | struct audlpa_event, list); |
| 360 | list_del(&drv_evt->list); |
| 361 | kfree(drv_evt); |
| 362 | } |
| 363 | list_for_each_safe(ptr, next, &audio->free_event_queue) { |
| 364 | drv_evt = list_first_entry(&audio->free_event_queue, |
| 365 | struct audlpa_event, list); |
| 366 | list_del(&drv_evt->list); |
| 367 | kfree(drv_evt); |
| 368 | } |
| 369 | spin_unlock(&audio->event_queue_lock); |
| 370 | |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | static long audlpa_process_event_req(struct audio *audio, void __user *arg) |
| 375 | { |
| 376 | long rc; |
| 377 | struct msm_audio_event usr_evt; |
| 378 | struct audlpa_event *drv_evt = NULL; |
| 379 | int timeout; |
| 380 | |
| 381 | if (copy_from_user(&usr_evt, arg, sizeof(struct msm_audio_event))) |
| 382 | return -EFAULT; |
| 383 | |
| 384 | timeout = (int) usr_evt.timeout_ms; |
| 385 | |
| 386 | if (timeout > 0) { |
| 387 | rc = wait_event_interruptible_timeout( |
| 388 | audio->event_wait, audlpa_events_pending(audio), |
| 389 | msecs_to_jiffies(timeout)); |
| 390 | if (rc == 0) |
| 391 | return -ETIMEDOUT; |
| 392 | } else { |
| 393 | rc = wait_event_interruptible( |
| 394 | audio->event_wait, audlpa_events_pending(audio)); |
| 395 | } |
| 396 | |
| 397 | if (rc < 0) |
| 398 | return rc; |
| 399 | |
| 400 | if (audio->event_abort) { |
| 401 | audio->event_abort = 0; |
| 402 | return -ENODEV; |
| 403 | } |
| 404 | |
| 405 | rc = 0; |
| 406 | |
| 407 | spin_lock(&audio->event_queue_lock); |
| 408 | if (!list_empty(&audio->event_queue)) { |
| 409 | drv_evt = list_first_entry(&audio->event_queue, |
| 410 | struct audlpa_event, list); |
| 411 | list_del(&drv_evt->list); |
| 412 | } |
| 413 | if (drv_evt) { |
| 414 | usr_evt.event_type = drv_evt->event_type; |
| 415 | usr_evt.event_payload = drv_evt->payload; |
| 416 | list_add_tail(&drv_evt->list, &audio->free_event_queue); |
| 417 | } else |
| 418 | rc = -1; |
| 419 | spin_unlock(&audio->event_queue_lock); |
| 420 | |
Vasudeva Rao Thumati | 86edf6c | 2011-07-06 16:25:13 +0530 | [diff] [blame^] | 421 | if (drv_evt && (drv_evt->event_type == AUDIO_EVENT_WRITE_DONE || |
| 422 | drv_evt->event_type == AUDIO_EVENT_READ_DONE)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 423 | pr_debug("%s: AUDIO_EVENT_WRITE_DONE completing\n", __func__); |
| 424 | mutex_lock(&audio->lock); |
| 425 | audlpa_pmem_fixup(audio, drv_evt->payload.aio_buf.buf_addr, |
| 426 | drv_evt->payload.aio_buf.buf_len, 0); |
| 427 | mutex_unlock(&audio->lock); |
| 428 | } |
| 429 | if (!rc && copy_to_user(arg, &usr_evt, sizeof(usr_evt))) |
| 430 | rc = -EFAULT; |
| 431 | |
| 432 | return rc; |
| 433 | } |
| 434 | |
| 435 | static int audlpa_pmem_check(struct audio *audio, |
| 436 | void *vaddr, unsigned long len) |
| 437 | { |
| 438 | struct audlpa_pmem_region *region_elt; |
| 439 | struct audlpa_pmem_region t = { .vaddr = vaddr, .len = len }; |
| 440 | |
| 441 | list_for_each_entry(region_elt, &audio->pmem_region_queue, list) { |
| 442 | if (CONTAINS(region_elt, &t) || CONTAINS(&t, region_elt) || |
| 443 | OVERLAPS(region_elt, &t)) { |
| 444 | pr_err("%s: region (vaddr %p len %ld)" |
| 445 | " clashes with registered region" |
| 446 | " (vaddr %p paddr %p len %ld)\n", |
| 447 | __func__, vaddr, len, |
| 448 | region_elt->vaddr, |
| 449 | (void *)region_elt->paddr, |
| 450 | region_elt->len); |
| 451 | return -EINVAL; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | static int audlpa_pmem_add(struct audio *audio, |
| 459 | struct msm_audio_pmem_info *info) |
| 460 | { |
| 461 | unsigned long paddr, kvaddr, len; |
| 462 | struct file *file; |
| 463 | struct audlpa_pmem_region *region; |
| 464 | int rc = -EINVAL; |
| 465 | |
| 466 | pr_debug("%s:\n", __func__); |
| 467 | region = kmalloc(sizeof(*region), GFP_KERNEL); |
| 468 | |
| 469 | if (!region) { |
| 470 | rc = -ENOMEM; |
| 471 | goto end; |
| 472 | } |
| 473 | |
| 474 | if (get_pmem_file(info->fd, &paddr, &kvaddr, &len, &file)) { |
| 475 | kfree(region); |
| 476 | goto end; |
| 477 | } |
| 478 | |
| 479 | rc = audlpa_pmem_check(audio, info->vaddr, len); |
| 480 | if (rc < 0) { |
| 481 | put_pmem_file(file); |
| 482 | kfree(region); |
| 483 | goto end; |
| 484 | } |
| 485 | |
| 486 | region->vaddr = info->vaddr; |
| 487 | region->fd = info->fd; |
| 488 | region->paddr = paddr; |
| 489 | region->kvaddr = kvaddr; |
| 490 | region->len = len; |
| 491 | region->file = file; |
| 492 | region->ref_cnt = 0; |
| 493 | pr_debug("%s: add region paddr %lx vaddr %p, len %lu\n", __func__, |
| 494 | region->paddr, region->vaddr, |
| 495 | region->len); |
| 496 | list_add_tail(®ion->list, &audio->pmem_region_queue); |
| 497 | rc = q6asm_memory_map(audio->ac, (uint32_t)paddr, IN, (uint32_t)len, 1); |
| 498 | if (rc < 0) |
| 499 | pr_err("%s: memory map failed\n", __func__); |
| 500 | end: |
| 501 | return rc; |
| 502 | } |
| 503 | |
| 504 | static int audlpa_pmem_remove(struct audio *audio, |
| 505 | struct msm_audio_pmem_info *info) |
| 506 | { |
| 507 | struct audlpa_pmem_region *region; |
| 508 | struct list_head *ptr, *next; |
| 509 | int rc = -EINVAL; |
| 510 | |
| 511 | list_for_each_safe(ptr, next, &audio->pmem_region_queue) { |
| 512 | region = list_entry(ptr, struct audlpa_pmem_region, list); |
| 513 | |
| 514 | if ((region != NULL) && (region->fd == info->fd) && |
| 515 | (region->vaddr == info->vaddr)) { |
| 516 | if (region->ref_cnt) { |
| 517 | pr_debug("%s: region %p in use ref_cnt %d\n", |
| 518 | __func__, region, region->ref_cnt); |
| 519 | break; |
| 520 | } |
| 521 | rc = q6asm_memory_unmap(audio->ac, |
| 522 | (uint32_t)region->paddr, |
| 523 | IN); |
| 524 | if (rc < 0) |
| 525 | pr_err("%s: memory unmap failed\n", __func__); |
| 526 | |
| 527 | list_del(®ion->list); |
| 528 | put_pmem_file(region->file); |
| 529 | kfree(region); |
| 530 | rc = 0; |
| 531 | break; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | return rc; |
| 536 | } |
| 537 | |
| 538 | static int audlpa_pmem_lookup_vaddr(struct audio *audio, void *addr, |
| 539 | unsigned long len, struct audlpa_pmem_region **region) |
| 540 | { |
| 541 | struct audlpa_pmem_region *region_elt; |
| 542 | |
| 543 | int match_count = 0; |
| 544 | |
| 545 | *region = NULL; |
| 546 | |
| 547 | /* returns physical address or zero */ |
| 548 | list_for_each_entry(region_elt, &audio->pmem_region_queue, |
| 549 | list) { |
| 550 | if (addr >= region_elt->vaddr && |
| 551 | addr < region_elt->vaddr + region_elt->len && |
| 552 | addr + len <= region_elt->vaddr + region_elt->len) { |
| 553 | /* offset since we could pass vaddr inside a registerd |
| 554 | * pmem buffer |
| 555 | */ |
| 556 | |
| 557 | match_count++; |
| 558 | if (!*region) |
| 559 | *region = region_elt; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | if (match_count > 1) { |
| 564 | pr_err("%s: multiple hits for vaddr %p, len %ld\n", __func__, |
| 565 | addr, len); |
| 566 | list_for_each_entry(region_elt, |
| 567 | &audio->pmem_region_queue, list) { |
| 568 | if (addr >= region_elt->vaddr && |
| 569 | addr < region_elt->vaddr + region_elt->len && |
| 570 | addr + len <= region_elt->vaddr + region_elt->len) |
| 571 | pr_err("%s: \t%p, %ld --> %p\n", __func__, |
| 572 | region_elt->vaddr, region_elt->len, |
| 573 | (void *)region_elt->paddr); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | return *region ? 0 : -1; |
| 578 | } |
| 579 | |
| 580 | unsigned long audlpa_pmem_fixup(struct audio *audio, void *addr, |
| 581 | unsigned long len, int ref_up) |
| 582 | { |
| 583 | struct audlpa_pmem_region *region; |
| 584 | unsigned long paddr; |
| 585 | int ret; |
| 586 | |
| 587 | ret = audlpa_pmem_lookup_vaddr(audio, addr, len, ®ion); |
| 588 | if (ret) { |
| 589 | pr_err("%s: lookup (%p, %ld) failed\n", __func__, addr, len); |
| 590 | return 0; |
| 591 | } |
| 592 | if (ref_up) |
| 593 | region->ref_cnt++; |
| 594 | else |
| 595 | region->ref_cnt--; |
| 596 | paddr = region->paddr + (addr - region->vaddr); |
| 597 | return paddr; |
| 598 | } |
| 599 | |
| 600 | /* audio -> lock must be held at this point */ |
| 601 | static int audlpa_aio_buf_add(struct audio *audio, unsigned dir, |
| 602 | void __user *arg) |
| 603 | { |
| 604 | struct audlpa_buffer_node *buf_node; |
| 605 | |
| 606 | buf_node = kmalloc(sizeof(*buf_node), GFP_KERNEL); |
| 607 | |
| 608 | if (!buf_node) |
| 609 | return -ENOMEM; |
| 610 | |
| 611 | if (copy_from_user(&buf_node->buf, arg, sizeof(buf_node->buf))) { |
| 612 | kfree(buf_node); |
| 613 | return -EFAULT; |
| 614 | } |
| 615 | |
| 616 | buf_node->paddr = audlpa_pmem_fixup( |
| 617 | audio, buf_node->buf.buf_addr, |
| 618 | buf_node->buf.buf_len, 1); |
| 619 | if (dir) { |
| 620 | /* write */ |
| 621 | if (!buf_node->paddr || |
| 622 | (buf_node->paddr & 0x1) || |
| 623 | (buf_node->buf.data_len & 0x1)) { |
| 624 | kfree(buf_node); |
| 625 | return -EINVAL; |
| 626 | } |
| 627 | list_add_tail(&buf_node->list, &audio->out_queue); |
| 628 | pr_debug("%s, Added to list: addr: %lx, length = %d\n", |
| 629 | __func__, buf_node->paddr, buf_node->buf.data_len); |
| 630 | audlpa_async_send_data(audio, 0, 0); |
| 631 | } else { |
| 632 | /* read */ |
Vasudeva Rao Thumati | 86edf6c | 2011-07-06 16:25:13 +0530 | [diff] [blame^] | 633 | kfree(buf_node); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 634 | } |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | static int config(struct audio *audio) |
| 639 | { |
| 640 | int rc = 0; |
| 641 | if (!audio->out_prefill) { |
| 642 | if (audio->codec_ops.set_params != NULL) { |
| 643 | rc = audio->codec_ops.set_params(audio); |
| 644 | audio->out_prefill = 1; |
| 645 | } |
| 646 | } |
| 647 | return rc; |
| 648 | } |
| 649 | |
| 650 | void q6_audlpa_out_cb(uint32_t opcode, uint32_t token, |
| 651 | uint32_t *payload, void *priv) |
| 652 | { |
| 653 | struct audio *audio = (struct audio *) priv; |
| 654 | |
| 655 | switch (opcode) { |
| 656 | case ASM_DATA_EVENT_WRITE_DONE: |
| 657 | pr_debug("%s: ASM_DATA_EVENT_WRITE_DONE, token = 0x%x\n", |
| 658 | __func__, token); |
| 659 | audlpa_async_send_data(audio, 1, token); |
| 660 | break; |
| 661 | case ASM_DATA_EVENT_EOS: |
| 662 | case ASM_DATA_CMDRSP_EOS: |
| 663 | pr_debug("%s: ASM_DATA_CMDRSP_EOS, teos = %d\n", __func__, |
| 664 | audio->teos); |
| 665 | if (audio->teos == 0) { |
| 666 | audio->teos = 1; |
| 667 | wake_up(&audio->write_wait); |
| 668 | } |
| 669 | break; |
| 670 | case ASM_SESSION_CMDRSP_GET_SESSION_TIME: |
| 671 | break; |
| 672 | default: |
| 673 | break; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | static long pcm_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 678 | { |
| 679 | pr_debug("%s: cmd = %d\n", __func__, cmd); |
| 680 | return -EINVAL; |
| 681 | } |
| 682 | |
| 683 | static int audlpa_set_pcm_params(void *data) |
| 684 | { |
| 685 | struct audio *audio = (struct audio *)data; |
| 686 | int rc; |
| 687 | |
| 688 | rc = q6asm_media_format_block_pcm(audio->ac, audio->out_sample_rate, |
| 689 | audio->out_channel_mode); |
| 690 | if (rc < 0) |
| 691 | pr_err("%s: Format block pcm failed\n", __func__); |
| 692 | return rc; |
| 693 | } |
| 694 | |
| 695 | static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 696 | { |
| 697 | struct audio *audio = file->private_data; |
| 698 | int rc = -EINVAL; |
| 699 | uint64_t timestamp; |
| 700 | uint64_t temp; |
| 701 | |
| 702 | pr_debug("%s: audio_ioctl() cmd = %d\n", __func__, cmd); |
| 703 | |
| 704 | if (cmd == AUDIO_GET_STATS) { |
| 705 | struct msm_audio_stats stats; |
| 706 | |
| 707 | pr_debug("%s: AUDIO_GET_STATS cmd\n", __func__); |
| 708 | memset(&stats, 0, sizeof(stats)); |
| 709 | timestamp = q6asm_get_session_time(audio->ac); |
| 710 | if (timestamp < 0) { |
| 711 | pr_err("%s: Get Session Time return value =%lld\n", |
| 712 | __func__, timestamp); |
| 713 | return -EAGAIN; |
| 714 | } |
| 715 | temp = (timestamp * 2 * audio->out_channel_mode); |
| 716 | temp = temp * (audio->out_sample_rate/1000); |
| 717 | temp = div_u64(temp, 1000); |
| 718 | audio->bytes_consumed = (uint32_t)(temp & 0xFFFFFFFF); |
| 719 | stats.byte_count = audio->bytes_consumed; |
| 720 | stats.unused[0] = (uint32_t)((temp >> 32) & 0xFFFFFFFF); |
| 721 | pr_debug("%s: bytes_consumed:lsb = %d, msb = %d," |
| 722 | "timestamp = %lld\n", __func__, |
| 723 | audio->bytes_consumed, stats.unused[0], timestamp); |
| 724 | if (copy_to_user((void *) arg, &stats, sizeof(stats))) |
| 725 | return -EFAULT; |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | switch (cmd) { |
| 730 | case AUDIO_ENABLE_AUDPP: |
| 731 | break; |
| 732 | |
| 733 | case AUDIO_SET_VOLUME: |
| 734 | break; |
| 735 | |
| 736 | case AUDIO_SET_PAN: |
| 737 | break; |
| 738 | |
| 739 | case AUDIO_SET_EQ: |
| 740 | break; |
| 741 | } |
| 742 | |
| 743 | if (cmd == AUDIO_GET_EVENT) { |
| 744 | pr_debug("%s: AUDIO_GET_EVENT\n", __func__); |
| 745 | if (mutex_trylock(&audio->get_event_lock)) { |
| 746 | rc = audlpa_process_event_req(audio, |
| 747 | (void __user *) arg); |
| 748 | mutex_unlock(&audio->get_event_lock); |
| 749 | } else |
| 750 | rc = -EBUSY; |
| 751 | return rc; |
| 752 | } |
| 753 | |
| 754 | if (cmd == AUDIO_ABORT_GET_EVENT) { |
| 755 | audio->event_abort = 1; |
| 756 | wake_up(&audio->event_wait); |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | mutex_lock(&audio->lock); |
| 761 | switch (cmd) { |
| 762 | case AUDIO_START: |
| 763 | pr_info("%s: AUDIO_START: Session %d\n", __func__, |
| 764 | audio->ac->session); |
| 765 | if (!audio->opened) { |
| 766 | pr_err("%s: Driver not opened\n", __func__); |
| 767 | rc = -EFAULT; |
| 768 | goto fail; |
| 769 | } |
| 770 | rc = config(audio); |
| 771 | if (rc) { |
| 772 | pr_err("%s: Out Configuration failed\n", __func__); |
| 773 | rc = -EFAULT; |
| 774 | goto fail; |
| 775 | } |
| 776 | |
| 777 | rc = audio_enable(audio); |
| 778 | if (rc) { |
| 779 | pr_err("%s: audio enable failed\n", __func__); |
| 780 | rc = -EFAULT; |
| 781 | goto fail; |
| 782 | } else { |
| 783 | struct asm_softpause_params param = { |
| 784 | .enable = SOFT_PAUSE_ENABLE, |
| 785 | .period = SOFT_PAUSE_PERIOD, |
| 786 | .step = SOFT_PAUSE_STEP, |
| 787 | .rampingcurve = SOFT_PAUSE_CURVE_LINEAR, |
| 788 | }; |
| 789 | audio->out_enabled = 1; |
| 790 | audio->out_needed = 1; |
| 791 | rc = q6asm_set_volume(audio->ac, audio->volume); |
| 792 | if (rc < 0) |
| 793 | pr_err("%s: Send Volume command failed rc=%d\n", |
| 794 | __func__, rc); |
| 795 | rc = q6asm_set_softpause(audio->ac, ¶m); |
| 796 | if (rc < 0) |
| 797 | pr_err("%s: Send SoftPause Param failed rc=%d\n", |
| 798 | __func__, rc); |
| 799 | rc = q6asm_set_lrgain(audio->ac, 0x2000, 0x2000); |
| 800 | if (rc < 0) |
| 801 | pr_err("%s: Send channel gain failed rc=%d\n", |
| 802 | __func__, rc); |
| 803 | /* disable mute by default */ |
| 804 | rc = q6asm_set_mute(audio->ac, 0); |
| 805 | if (rc < 0) |
| 806 | pr_err("%s: Send mute command failed rc=%d\n", |
| 807 | __func__, rc); |
| 808 | if (!list_empty(&audio->out_queue)) |
| 809 | pr_err("%s: write_list is not empty!!!\n", |
| 810 | __func__); |
| 811 | if (audio->stopped == 1) |
| 812 | audio->stopped = 0; |
| 813 | audlpa_prevent_sleep(audio); |
| 814 | } |
| 815 | break; |
| 816 | |
| 817 | case AUDIO_STOP: |
| 818 | pr_info("%s: AUDIO_STOP: session_id:%d\n", __func__, |
| 819 | audio->ac->session); |
| 820 | audio->stopped = 1; |
| 821 | audlpa_async_flush(audio); |
| 822 | audio->out_enabled = 0; |
| 823 | audio->out_needed = 0; |
| 824 | audio->drv_status &= ~ADRV_STATUS_PAUSE; |
| 825 | audlpa_allow_sleep(audio); |
| 826 | break; |
| 827 | |
| 828 | case AUDIO_FLUSH: |
| 829 | pr_debug("%s: AUDIO_FLUSH: session_id:%d\n", __func__, |
| 830 | audio->ac->session); |
| 831 | audio->wflush = 1; |
| 832 | if (audio->out_enabled) |
| 833 | audlpa_async_flush(audio); |
| 834 | else |
| 835 | audio->wflush = 0; |
| 836 | audio->wflush = 0; |
| 837 | break; |
| 838 | |
| 839 | case AUDIO_SET_CONFIG:{ |
| 840 | struct msm_audio_config config; |
| 841 | pr_debug("%s: AUDIO_SET_CONFIG\n", __func__); |
| 842 | if (copy_from_user(&config, (void *) arg, sizeof(config))) { |
| 843 | rc = -EFAULT; |
| 844 | pr_err("%s: ERROR: copy from user\n", __func__); |
| 845 | break; |
| 846 | } |
| 847 | if (!((config.channel_count == 1) || |
| 848 | (config.channel_count == 2))) { |
| 849 | rc = -EINVAL; |
| 850 | pr_err("%s: ERROR: config.channel_count == %d\n", |
| 851 | __func__, config.channel_count); |
| 852 | break; |
| 853 | } |
| 854 | |
| 855 | if (!((config.bits == 8) || (config.bits == 16) || |
| 856 | (config.bits == 24))) { |
| 857 | rc = -EINVAL; |
| 858 | pr_err("%s: ERROR: config.bits = %d\n", __func__, |
| 859 | config.bits); |
| 860 | break; |
| 861 | } |
| 862 | audio->out_sample_rate = config.sample_rate; |
| 863 | audio->out_channel_mode = config.channel_count; |
| 864 | audio->out_bits = config.bits; |
| 865 | audio->buffer_count = config.buffer_count; |
| 866 | audio->buffer_size = config.buffer_size; |
| 867 | rc = 0; |
| 868 | break; |
| 869 | } |
| 870 | |
| 871 | case AUDIO_GET_CONFIG:{ |
| 872 | struct msm_audio_config config; |
| 873 | config.buffer_count = audio->buffer_count; |
| 874 | config.buffer_size = audio->buffer_size; |
| 875 | config.sample_rate = audio->out_sample_rate; |
| 876 | config.channel_count = audio->out_channel_mode; |
| 877 | config.bits = audio->out_bits; |
| 878 | |
| 879 | config.meta_field = 0; |
| 880 | config.unused[0] = 0; |
| 881 | config.unused[1] = 0; |
| 882 | config.unused[2] = 0; |
| 883 | if (copy_to_user((void *) arg, &config, sizeof(config))) |
| 884 | rc = -EFAULT; |
| 885 | else |
| 886 | rc = 0; |
| 887 | break; |
| 888 | } |
| 889 | |
| 890 | case AUDIO_PAUSE: |
| 891 | pr_debug("%s: AUDIO_PAUSE %ld\n", __func__, arg); |
| 892 | if (arg == 1) { |
| 893 | rc = audlpa_pause(audio); |
| 894 | if (rc < 0) |
| 895 | pr_err("%s: pause FAILED rc=%d\n", __func__, |
| 896 | rc); |
| 897 | audio->drv_status |= ADRV_STATUS_PAUSE; |
| 898 | } else if (arg == 0) { |
| 899 | if (audio->drv_status & ADRV_STATUS_PAUSE) { |
| 900 | rc = audio_enable(audio); |
| 901 | if (rc) |
| 902 | pr_err("%s: audio enable failed\n", |
| 903 | __func__); |
| 904 | else { |
| 905 | audio->drv_status &= ~ADRV_STATUS_PAUSE; |
| 906 | audio->out_enabled = 1; |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | break; |
| 911 | |
| 912 | case AUDIO_REGISTER_PMEM: { |
| 913 | struct msm_audio_pmem_info info; |
| 914 | pr_debug("%s: AUDIO_REGISTER_PMEM\n", __func__); |
| 915 | if (copy_from_user(&info, (void *) arg, sizeof(info))) |
| 916 | rc = -EFAULT; |
| 917 | else |
| 918 | rc = audlpa_pmem_add(audio, &info); |
| 919 | break; |
| 920 | } |
| 921 | |
| 922 | case AUDIO_DEREGISTER_PMEM: { |
| 923 | struct msm_audio_pmem_info info; |
| 924 | pr_debug("%s: AUDIO_DEREGISTER_PMEM\n", __func__); |
| 925 | if (copy_from_user(&info, (void *) arg, sizeof(info))) |
| 926 | rc = -EFAULT; |
| 927 | else |
| 928 | rc = audlpa_pmem_remove(audio, &info); |
| 929 | break; |
| 930 | } |
| 931 | case AUDIO_ASYNC_WRITE: |
| 932 | pr_debug("%s: AUDIO_ASYNC_WRITE\n", __func__); |
| 933 | if (audio->drv_status & ADRV_STATUS_FSYNC) |
| 934 | rc = -EBUSY; |
| 935 | else |
| 936 | rc = audlpa_aio_buf_add(audio, 1, (void __user *) arg); |
| 937 | break; |
| 938 | |
| 939 | case AUDIO_GET_SESSION_ID: |
| 940 | if (copy_to_user((void *) arg, &audio->ac->session, |
| 941 | sizeof(unsigned short))) |
| 942 | return -EFAULT; |
| 943 | rc = 0; |
| 944 | break; |
| 945 | |
| 946 | default: |
| 947 | rc = audio->codec_ops.ioctl(file, cmd, arg); |
| 948 | } |
| 949 | fail: |
| 950 | mutex_unlock(&audio->lock); |
| 951 | return rc; |
| 952 | } |
| 953 | |
| 954 | /* Only useful in tunnel-mode */ |
| 955 | int audlpa_async_fsync(struct audio *audio) |
| 956 | { |
| 957 | int rc = 0; |
| 958 | |
| 959 | pr_info("%s:Session %d\n", __func__, audio->ac->session); |
| 960 | |
| 961 | /* Blocking client sends more data */ |
| 962 | mutex_lock(&audio->lock); |
| 963 | audio->drv_status |= ADRV_STATUS_FSYNC; |
| 964 | mutex_unlock(&audio->lock); |
| 965 | |
| 966 | mutex_lock(&audio->write_lock); |
| 967 | audio->teos = 0; |
| 968 | |
| 969 | rc = wait_event_interruptible(audio->write_wait, |
| 970 | ((list_empty(&audio->out_queue)) || |
| 971 | audio->wflush || audio->stopped)); |
| 972 | |
| 973 | if (audio->wflush || audio->stopped) |
| 974 | goto flush_event; |
| 975 | |
| 976 | if (rc < 0) { |
| 977 | pr_err("%s: wait event for list_empty failed, rc = %d\n", |
| 978 | __func__, rc); |
| 979 | goto done; |
| 980 | } |
| 981 | |
| 982 | rc = q6asm_cmd(audio->ac, CMD_EOS); |
| 983 | |
| 984 | if (rc < 0) { |
| 985 | pr_err("%s: q6asm_cmd failed, rc = %d", __func__, rc); |
| 986 | goto done; |
| 987 | } |
| 988 | rc = wait_event_interruptible_timeout(audio->write_wait, |
| 989 | (audio->teos || audio->wflush || |
| 990 | audio->stopped), 5*HZ); |
| 991 | |
| 992 | if (rc < 0) { |
| 993 | pr_err("%s: wait event for teos failed, rc = %d\n", __func__, |
| 994 | rc); |
| 995 | goto done; |
| 996 | } |
| 997 | |
| 998 | if (audio->teos == 1) { |
| 999 | rc = audio_enable(audio); |
| 1000 | if (rc) |
| 1001 | pr_err("%s: audio enable failed\n", __func__); |
| 1002 | else { |
| 1003 | audio->drv_status &= ~ADRV_STATUS_PAUSE; |
| 1004 | audio->out_enabled = 1; |
| 1005 | audio->out_needed = 1; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | flush_event: |
| 1010 | if (audio->stopped || audio->wflush) |
| 1011 | rc = -EBUSY; |
| 1012 | |
| 1013 | done: |
| 1014 | mutex_unlock(&audio->write_lock); |
| 1015 | mutex_lock(&audio->lock); |
| 1016 | audio->drv_status &= ~ADRV_STATUS_FSYNC; |
| 1017 | mutex_unlock(&audio->lock); |
| 1018 | |
| 1019 | return rc; |
| 1020 | } |
| 1021 | |
| 1022 | int audlpa_fsync(struct file *file, int datasync) |
| 1023 | { |
| 1024 | struct audio *audio = file->private_data; |
| 1025 | |
| 1026 | return audlpa_async_fsync(audio); |
| 1027 | } |
| 1028 | |
| 1029 | static void audlpa_reset_pmem_region(struct audio *audio) |
| 1030 | { |
| 1031 | struct audlpa_pmem_region *region; |
| 1032 | struct list_head *ptr, *next; |
| 1033 | |
| 1034 | list_for_each_safe(ptr, next, &audio->pmem_region_queue) { |
| 1035 | region = list_entry(ptr, struct audlpa_pmem_region, list); |
| 1036 | list_del(®ion->list); |
| 1037 | put_pmem_file(region->file); |
| 1038 | kfree(region); |
| 1039 | } |
| 1040 | |
| 1041 | return; |
| 1042 | } |
| 1043 | |
| 1044 | static void audlpa_unmap_pmem_region(struct audio *audio) |
| 1045 | { |
| 1046 | struct audlpa_pmem_region *region; |
| 1047 | struct list_head *ptr, *next; |
| 1048 | int rc = -EINVAL; |
| 1049 | |
| 1050 | pr_debug("%s:\n", __func__); |
| 1051 | list_for_each_safe(ptr, next, &audio->pmem_region_queue) { |
| 1052 | region = list_entry(ptr, struct audlpa_pmem_region, list); |
| 1053 | pr_debug("%s: phy_address = 0x%lx\n", __func__, region->paddr); |
| 1054 | if (region != NULL) { |
| 1055 | rc = q6asm_memory_unmap(audio->ac, |
| 1056 | (uint32_t)region->paddr, IN); |
| 1057 | if (rc < 0) |
| 1058 | pr_err("%s: memory unmap failed\n", __func__); |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | static int audio_release(struct inode *inode, struct file *file) |
| 1064 | { |
| 1065 | struct audio *audio = file->private_data; |
| 1066 | |
| 1067 | pr_info("%s: audio instance 0x%08x freeing, session %d\n", __func__, |
| 1068 | (int)audio, audio->ac->session); |
| 1069 | |
| 1070 | mutex_lock(&audio->lock); |
| 1071 | audio->wflush = 1; |
| 1072 | if (audio->out_enabled) |
| 1073 | audlpa_async_flush(audio); |
| 1074 | audio->wflush = 0; |
| 1075 | audlpa_unmap_pmem_region(audio); |
| 1076 | audio_disable(audio); |
| 1077 | msm_clear_session_id(audio->ac->session); |
| 1078 | auddev_unregister_evt_listner(AUDDEV_CLNT_DEC, audio->ac->session); |
| 1079 | q6asm_audio_client_free(audio->ac); |
| 1080 | audlpa_reset_pmem_region(audio); |
| 1081 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 1082 | unregister_early_suspend(&audio->suspend_ctl.node); |
| 1083 | #endif |
| 1084 | audio->opened = 0; |
| 1085 | audio->out_enabled = 0; |
| 1086 | audio->out_prefill = 0; |
| 1087 | audio->event_abort = 1; |
| 1088 | wake_up(&audio->event_wait); |
| 1089 | audlpa_reset_event_queue(audio); |
| 1090 | pmem_kfree(audio->phys); |
| 1091 | if (audio->stopped == 0) |
| 1092 | audlpa_allow_sleep(audio); |
| 1093 | wake_lock_destroy(&audio->wakelock); |
| 1094 | |
| 1095 | mutex_unlock(&audio->lock); |
| 1096 | #ifdef CONFIG_DEBUG_FS |
| 1097 | if (audio->dentry) |
| 1098 | debugfs_remove(audio->dentry); |
| 1099 | #endif |
| 1100 | kfree(audio); |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
| 1104 | static void audlpa_post_event(struct audio *audio, int type, |
| 1105 | union msm_audio_event_payload payload) |
| 1106 | { |
| 1107 | struct audlpa_event *e_node = NULL; |
| 1108 | |
| 1109 | spin_lock(&audio->event_queue_lock); |
| 1110 | |
| 1111 | pr_debug("%s:\n", __func__); |
| 1112 | if (!list_empty(&audio->free_event_queue)) { |
| 1113 | e_node = list_first_entry(&audio->free_event_queue, |
| 1114 | struct audlpa_event, list); |
| 1115 | list_del(&e_node->list); |
| 1116 | } else { |
| 1117 | e_node = kmalloc(sizeof(struct audlpa_event), GFP_ATOMIC); |
| 1118 | if (!e_node) { |
| 1119 | pr_err("%s: No mem to post event %d\n", __func__, type); |
| 1120 | return; |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | e_node->event_type = type; |
| 1125 | e_node->payload = payload; |
| 1126 | |
| 1127 | list_add_tail(&e_node->list, &audio->event_queue); |
| 1128 | spin_unlock(&audio->event_queue_lock); |
| 1129 | wake_up(&audio->event_wait); |
| 1130 | } |
| 1131 | |
| 1132 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 1133 | static void audlpa_suspend(struct early_suspend *h) |
| 1134 | { |
| 1135 | struct audlpa_suspend_ctl *ctl = |
| 1136 | container_of(h, struct audlpa_suspend_ctl, node); |
| 1137 | union msm_audio_event_payload payload; |
| 1138 | |
| 1139 | pr_debug("%s:\n", __func__); |
| 1140 | audlpa_post_event(ctl->audio, AUDIO_EVENT_SUSPEND, payload); |
| 1141 | } |
| 1142 | |
| 1143 | static void audlpa_resume(struct early_suspend *h) |
| 1144 | { |
| 1145 | struct audlpa_suspend_ctl *ctl = |
| 1146 | container_of(h, struct audlpa_suspend_ctl, node); |
| 1147 | union msm_audio_event_payload payload; |
| 1148 | |
| 1149 | pr_debug("%s:\n", __func__); |
| 1150 | audlpa_post_event(ctl->audio, AUDIO_EVENT_RESUME, payload); |
| 1151 | } |
| 1152 | #endif |
| 1153 | |
| 1154 | #ifdef CONFIG_DEBUG_FS |
| 1155 | static ssize_t audlpa_debug_open(struct inode *inode, struct file *file) |
| 1156 | { |
| 1157 | file->private_data = inode->i_private; |
| 1158 | return 0; |
| 1159 | } |
| 1160 | |
| 1161 | static ssize_t audlpa_debug_read(struct file *file, char __user *buf, |
| 1162 | size_t count, loff_t *ppos) |
| 1163 | { |
| 1164 | const int debug_bufmax = 4096; |
| 1165 | static char buffer[4096]; |
| 1166 | int n = 0; |
| 1167 | struct audio *audio = file->private_data; |
| 1168 | |
| 1169 | mutex_lock(&audio->lock); |
| 1170 | n = scnprintf(buffer, debug_bufmax, "opened %d\n", audio->opened); |
| 1171 | n += scnprintf(buffer + n, debug_bufmax - n, |
| 1172 | "out_enabled %d\n", audio->out_enabled); |
| 1173 | n += scnprintf(buffer + n, debug_bufmax - n, |
| 1174 | "stopped %d\n", audio->stopped); |
| 1175 | n += scnprintf(buffer + n, debug_bufmax - n, |
| 1176 | "volume %x\n", audio->volume); |
| 1177 | n += scnprintf(buffer + n, debug_bufmax - n, |
| 1178 | "sample rate %d\n", |
| 1179 | audio->out_sample_rate); |
| 1180 | n += scnprintf(buffer + n, debug_bufmax - n, |
| 1181 | "channel mode %d\n", |
| 1182 | audio->out_channel_mode); |
| 1183 | mutex_unlock(&audio->lock); |
| 1184 | /* Following variables are only useful for debugging when |
| 1185 | * when playback halts unexpectedly. Thus, no mutual exclusion |
| 1186 | * enforced |
| 1187 | */ |
| 1188 | n += scnprintf(buffer + n, debug_bufmax - n, |
| 1189 | "wflush %d\n", audio->wflush); |
| 1190 | n += scnprintf(buffer + n, debug_bufmax - n, |
| 1191 | "running %d\n", audio->running); |
| 1192 | n += scnprintf(buffer + n, debug_bufmax - n, |
| 1193 | "out_needed %d\n", audio->out_needed); |
| 1194 | buffer[n] = 0; |
| 1195 | return simple_read_from_buffer(buf, count, ppos, buffer, n); |
| 1196 | } |
| 1197 | |
| 1198 | static const struct file_operations audlpa_debug_fops = { |
| 1199 | .read = audlpa_debug_read, |
| 1200 | .open = audlpa_debug_open, |
| 1201 | }; |
| 1202 | #endif |
| 1203 | |
| 1204 | static int audio_open(struct inode *inode, struct file *file) |
| 1205 | { |
| 1206 | struct audio *audio = NULL; |
| 1207 | int rc, i, dec_attrb = 0; |
| 1208 | struct audlpa_event *e_node = NULL; |
| 1209 | #ifdef CONFIG_DEBUG_FS |
| 1210 | /* 4 bytes represents decoder number, 1 byte for terminate string */ |
| 1211 | char name[sizeof "msm_lpa_" + 5]; |
| 1212 | #endif |
| 1213 | char wake_lock_name[24]; |
| 1214 | |
| 1215 | /* Allocate audio instance, set to zero */ |
| 1216 | audio = kzalloc(sizeof(struct audio), GFP_KERNEL); |
| 1217 | if (!audio) { |
| 1218 | pr_err("%s: no memory to allocate audio instance\n", __func__); |
| 1219 | rc = -ENOMEM; |
| 1220 | goto done; |
| 1221 | } |
| 1222 | |
| 1223 | if ((file->f_mode & FMODE_WRITE) && !(file->f_mode & FMODE_READ)) { |
| 1224 | pr_debug("%s: Tunnel Mode playback\n", __func__); |
| 1225 | } else { |
| 1226 | kfree(audio); |
| 1227 | rc = -EACCES; |
| 1228 | goto done; |
| 1229 | } |
| 1230 | |
| 1231 | /* Allocate the decoder based on inode minor number*/ |
| 1232 | audio->minor_no = iminor(inode); |
| 1233 | dec_attrb |= audlpa_decs[audio->minor_no].dec_attrb; |
| 1234 | audio->codec_ops.ioctl = audlpa_decs[audio->minor_no].ioctl; |
| 1235 | audio->codec_ops.set_params = audlpa_decs[audio->minor_no].set_params; |
| 1236 | audio->buffer_size = BUFSZ; |
| 1237 | audio->buffer_count = MAX_BUF; |
| 1238 | |
| 1239 | audio->ac = q6asm_audio_client_alloc((app_cb)q6_audlpa_out_cb, |
| 1240 | (void *)audio); |
| 1241 | if (!audio->ac) { |
| 1242 | pr_err("%s: Could not allocate memory for lpa client\n", |
| 1243 | __func__); |
| 1244 | rc = -ENOMEM; |
| 1245 | goto err; |
| 1246 | } |
| 1247 | rc = q6asm_open_write(audio->ac, FORMAT_LINEAR_PCM); |
| 1248 | if (rc < 0) { |
| 1249 | pr_err("%s: lpa out open failed\n", __func__); |
| 1250 | goto err; |
| 1251 | } |
| 1252 | |
| 1253 | pr_debug("%s: Set mode to AIO session[%d]\n", |
| 1254 | __func__, |
| 1255 | audio->ac->session); |
| 1256 | rc = q6asm_set_io_mode(audio->ac, ASYNC_IO_MODE); |
| 1257 | if (rc < 0) |
| 1258 | pr_err("%s: Set IO mode failed\n", __func__); |
| 1259 | |
| 1260 | |
| 1261 | /* Initialize all locks of audio instance */ |
| 1262 | mutex_init(&audio->lock); |
| 1263 | mutex_init(&audio->write_lock); |
| 1264 | mutex_init(&audio->get_event_lock); |
| 1265 | spin_lock_init(&audio->dsp_lock); |
| 1266 | init_waitqueue_head(&audio->write_wait); |
| 1267 | INIT_LIST_HEAD(&audio->out_queue); |
| 1268 | INIT_LIST_HEAD(&audio->pmem_region_queue); |
| 1269 | INIT_LIST_HEAD(&audio->free_event_queue); |
| 1270 | INIT_LIST_HEAD(&audio->event_queue); |
| 1271 | init_waitqueue_head(&audio->wait); |
| 1272 | init_waitqueue_head(&audio->event_wait); |
| 1273 | spin_lock_init(&audio->event_queue_lock); |
| 1274 | snprintf(wake_lock_name, sizeof wake_lock_name, "audio_lpa_%x", |
| 1275 | audio->ac->session); |
| 1276 | wake_lock_init(&audio->wakelock, WAKE_LOCK_SUSPEND, wake_lock_name); |
| 1277 | |
| 1278 | audio->out_sample_rate = 44100; |
| 1279 | audio->out_channel_mode = 2; |
| 1280 | audio->out_bits = 16; |
| 1281 | audio->volume = 0x2000; |
| 1282 | |
| 1283 | file->private_data = audio; |
| 1284 | audio->opened = 1; |
| 1285 | audio->out_enabled = 0; |
| 1286 | audio->out_prefill = 0; |
| 1287 | audio->bytes_consumed = 0; |
| 1288 | |
| 1289 | audio->device_events = AUDDEV_EVT_STREAM_VOL_CHG; |
| 1290 | audio->drv_status &= ~ADRV_STATUS_PAUSE; |
| 1291 | |
| 1292 | rc = auddev_register_evt_listner(audio->device_events, |
| 1293 | AUDDEV_CLNT_DEC, |
| 1294 | audio->ac->session, |
| 1295 | lpa_listner, |
| 1296 | (void *)audio); |
| 1297 | if (rc) { |
| 1298 | pr_err("%s: failed to register listner\n", __func__); |
| 1299 | goto err; |
| 1300 | } |
| 1301 | |
| 1302 | #ifdef CONFIG_DEBUG_FS |
| 1303 | snprintf(name, sizeof name, "msm_lpa_%04x", audio->ac->session); |
| 1304 | audio->dentry = debugfs_create_file(name, S_IFREG | S_IRUGO, |
| 1305 | NULL, (void *) audio, &audlpa_debug_fops); |
| 1306 | |
| 1307 | if (IS_ERR(audio->dentry)) |
| 1308 | pr_err("%s: debugfs_create_file failed\n", __func__); |
| 1309 | #endif |
| 1310 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 1311 | audio->suspend_ctl.node.level = EARLY_SUSPEND_LEVEL_DISABLE_FB; |
| 1312 | audio->suspend_ctl.node.resume = audlpa_resume; |
| 1313 | audio->suspend_ctl.node.suspend = audlpa_suspend; |
| 1314 | audio->suspend_ctl.audio = audio; |
| 1315 | register_early_suspend(&audio->suspend_ctl.node); |
| 1316 | #endif |
| 1317 | for (i = 0; i < AUDLPA_EVENT_NUM; i++) { |
| 1318 | e_node = kmalloc(sizeof(struct audlpa_event), GFP_KERNEL); |
| 1319 | if (e_node) |
| 1320 | list_add_tail(&e_node->list, &audio->free_event_queue); |
| 1321 | else { |
| 1322 | pr_err("%s: event pkt alloc failed\n", __func__); |
| 1323 | break; |
| 1324 | } |
| 1325 | } |
| 1326 | pr_info("%s: audio instance 0x%08x created session[%d]\n", __func__, |
| 1327 | (int)audio, |
| 1328 | audio->ac->session); |
| 1329 | done: |
| 1330 | return rc; |
| 1331 | err: |
| 1332 | q6asm_audio_client_free(audio->ac); |
| 1333 | pmem_kfree(audio->phys); |
| 1334 | kfree(audio); |
| 1335 | return rc; |
| 1336 | } |
| 1337 | |
| 1338 | static const struct file_operations audio_lpa_fops = { |
| 1339 | .owner = THIS_MODULE, |
| 1340 | .open = audio_open, |
| 1341 | .release = audio_release, |
| 1342 | .unlocked_ioctl = audio_ioctl, |
| 1343 | .fsync = audlpa_fsync, |
| 1344 | }; |
| 1345 | |
| 1346 | static dev_t audlpa_devno; |
| 1347 | static struct class *audlpa_class; |
| 1348 | struct audlpa_device { |
| 1349 | const char *name; |
| 1350 | struct device *device; |
| 1351 | struct cdev cdev; |
| 1352 | }; |
| 1353 | |
| 1354 | static struct audlpa_device *audlpa_devices; |
| 1355 | |
| 1356 | static void audlpa_create(struct audlpa_device *adev, const char *name, |
| 1357 | struct device *parent, dev_t devt) |
| 1358 | { |
| 1359 | struct device *dev; |
| 1360 | int rc; |
| 1361 | |
| 1362 | dev = device_create(audlpa_class, parent, devt, "%s", name); |
| 1363 | if (IS_ERR(dev)) |
| 1364 | return; |
| 1365 | |
| 1366 | cdev_init(&adev->cdev, &audio_lpa_fops); |
| 1367 | adev->cdev.owner = THIS_MODULE; |
| 1368 | |
| 1369 | rc = cdev_add(&adev->cdev, devt, 1); |
| 1370 | if (rc < 0) { |
| 1371 | device_destroy(audlpa_class, devt); |
| 1372 | } else { |
| 1373 | adev->device = dev; |
| 1374 | adev->name = name; |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | static int __init audio_init(void) |
| 1379 | { |
| 1380 | int rc; |
| 1381 | int n = ARRAY_SIZE(audlpa_decs); |
| 1382 | |
| 1383 | audlpa_devices = kzalloc(sizeof(struct audlpa_device) * n, GFP_KERNEL); |
| 1384 | if (!audlpa_devices) |
| 1385 | return -ENOMEM; |
| 1386 | |
| 1387 | audlpa_class = class_create(THIS_MODULE, "audlpa"); |
| 1388 | if (IS_ERR(audlpa_class)) |
| 1389 | goto fail_create_class; |
| 1390 | |
| 1391 | rc = alloc_chrdev_region(&audlpa_devno, 0, n, "msm_audio_lpa"); |
| 1392 | if (rc < 0) |
| 1393 | goto fail_alloc_region; |
| 1394 | |
| 1395 | for (n = 0; n < ARRAY_SIZE(audlpa_decs); n++) { |
| 1396 | audlpa_create(audlpa_devices + n, |
| 1397 | audlpa_decs[n].name, NULL, |
| 1398 | MKDEV(MAJOR(audlpa_devno), n)); |
| 1399 | } |
| 1400 | |
| 1401 | return 0; |
| 1402 | |
| 1403 | fail_alloc_region: |
| 1404 | class_unregister(audlpa_class); |
| 1405 | return rc; |
| 1406 | fail_create_class: |
| 1407 | kfree(audlpa_devices); |
| 1408 | return -ENOMEM; |
| 1409 | } |
| 1410 | |
| 1411 | static void __exit audio_exit(void) |
| 1412 | { |
| 1413 | class_unregister(audlpa_class); |
| 1414 | kfree(audlpa_devices); |
| 1415 | } |
| 1416 | |
| 1417 | module_init(audio_init); |
| 1418 | module_exit(audio_exit); |
| 1419 | |
| 1420 | MODULE_DESCRIPTION("MSM LPA driver"); |
| 1421 | MODULE_LICENSE("GPL v2"); |