blob: 41a73878824d05c69e0f6076b25851d630214669 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* low power audio output device
2 *
3 * Copyright (C) 2008 Google, Inc.
4 * Copyright (C) 2008 HTC Corporation
Sidipotu Ashok1fe784b2012-02-16 09:44:40 +05305 * Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07006 *
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>
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +053031#include <linux/ion.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032#include <linux/list.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033#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
Sidipotu Ashok1fe784b2012-02-16 09:44:40 +053048#define MAX_BUF 4
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049#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
86struct audlpa_event {
87 struct list_head list;
88 int event_type;
89 union msm_audio_event_payload payload;
90};
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +053091struct audlpa_ion_region {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092 struct list_head list;
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +053093 struct ion_handle *handle;
94 struct ion_client *client;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070095 int fd;
96 void *vaddr;
97 unsigned long paddr;
98 unsigned long kvaddr;
99 unsigned long len;
100 unsigned ref_cnt;
101};
102
103struct audlpa_buffer_node {
104 struct list_head list;
105 struct msm_audio_aio_buf buf;
106 unsigned long paddr;
107};
108
109struct 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
116static void audlpa_post_event(struct audio *audio, int type,
117 union msm_audio_event_payload payload);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530118
119static unsigned long audlpa_ion_fixup(struct audio *audio, void *addr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120 unsigned long len, int ref_up);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530121static void audlpa_unmap_ion_region(struct audio *audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700122static void audlpa_async_send_data(struct audio *audio, unsigned needed,
123 uint32_t token);
124static int audlpa_pause(struct audio *audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125static long pcm_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
126static int audlpa_set_pcm_params(void *data);
127
128struct audlpa_dec audlpa_decs[] = {
129 {"msm_pcm_lp_dec", AUDDEC_DEC_PCM, &pcm_ioctl,
130 &audlpa_set_pcm_params},
131};
132
133static void lpa_listner(u32 evt_id, union auddev_evt_data *evt_payload,
134 void *private_data)
135{
136 struct audio *audio = (struct audio *) private_data;
137 int rc = 0;
138
139 switch (evt_id) {
140 case AUDDEV_EVT_STREAM_VOL_CHG:
141 audio->volume = evt_payload->session_vol;
142 pr_debug("%s: AUDDEV_EVT_STREAM_VOL_CHG, stream vol %d, "
143 "enabled = %d\n", __func__, audio->volume,
144 audio->out_enabled);
145 if (audio->out_enabled == 1) {
146 if (audio->ac) {
147 rc = q6asm_set_volume(audio->ac, audio->volume);
148 if (rc < 0) {
149 pr_err("%s: Send Volume command failed"
150 " rc=%d\n", __func__, rc);
151 }
152 }
153 }
154 break;
155 default:
156 pr_err("%s:ERROR:wrong event\n", __func__);
157 break;
158 }
159}
160
161static void audlpa_prevent_sleep(struct audio *audio)
162{
163 pr_debug("%s:\n", __func__);
164 wake_lock(&audio->wakelock);
165}
166
167static void audlpa_allow_sleep(struct audio *audio)
168{
169 pr_debug("%s:\n", __func__);
170 wake_unlock(&audio->wakelock);
171}
172
173/* must be called with audio->lock held */
174static int audio_enable(struct audio *audio)
175{
176 pr_debug("%s\n", __func__);
177
178 return q6asm_run(audio->ac, 0, 0, 0);
179
180}
181
182static void audlpa_async_flush(struct audio *audio)
183{
184 struct audlpa_buffer_node *buf_node;
185 struct list_head *ptr, *next;
186 union msm_audio_event_payload payload;
187 int rc = 0;
188
189 pr_debug("%s:out_enabled = %d, drv_status = 0x%x\n", __func__,
190 audio->out_enabled, audio->drv_status);
191 if (audio->out_enabled) {
192 list_for_each_safe(ptr, next, &audio->out_queue) {
193 buf_node = list_entry(ptr, struct audlpa_buffer_node,
194 list);
195 list_del(&buf_node->list);
196 payload.aio_buf = buf_node->buf;
197 audlpa_post_event(audio, AUDIO_EVENT_WRITE_DONE,
198 payload);
199 kfree(buf_node);
200 }
201 /* Implicitly issue a pause to the decoder before flushing if
202 it is not in pause state */
203 if (!(audio->drv_status & ADRV_STATUS_PAUSE)) {
204 rc = audlpa_pause(audio);
205 if (rc < 0)
206 pr_err("%s: pause cmd failed rc=%d\n", __func__,
207 rc);
208 }
209
210 rc = q6asm_cmd(audio->ac, CMD_FLUSH);
211 if (rc < 0)
212 pr_err("%s: flush cmd failed rc=%d\n", __func__, rc);
213
214 audio->drv_status &= ~ADRV_STATUS_OBUF_GIVEN;
215 audio->out_needed = 0;
216
217 if (audio->stopped == 0) {
218 rc = audio_enable(audio);
219 if (rc < 0)
220 pr_err("%s: audio enable failed\n", __func__);
221 else {
222 audio->out_enabled = 1;
223 audio->out_needed = 1;
224 if (audio->drv_status & ADRV_STATUS_PAUSE)
225 audio->drv_status &= ~ADRV_STATUS_PAUSE;
226 }
227 }
228 wake_up(&audio->write_wait);
229 }
230}
231
232/* must be called with audio->lock held */
233static int audio_disable(struct audio *audio)
234{
235 int rc = 0;
236
237 pr_debug("%s:%d %d\n", __func__, audio->opened, audio->out_enabled);
238
239 if (audio->opened) {
240 audio->out_enabled = 0;
241 audio->opened = 0;
242 rc = q6asm_cmd(audio->ac, CMD_CLOSE);
243 if (rc < 0)
244 pr_err("%s: CLOSE cmd failed\n", __func__);
245 else
246 pr_debug("%s: rxed CLOSE resp\n", __func__);
247 audio->drv_status &= ~ADRV_STATUS_OBUF_GIVEN;
248 wake_up(&audio->write_wait);
249 audio->out_needed = 0;
250 }
251 return rc;
252}
253static int audlpa_pause(struct audio *audio)
254{
255 int rc = 0;
256
257 pr_debug("%s, enabled = %d\n", __func__,
258 audio->out_enabled);
259 if (audio->out_enabled) {
260 rc = q6asm_cmd(audio->ac, CMD_PAUSE);
261 if (rc < 0)
262 pr_err("%s: pause cmd failed rc=%d\n", __func__, rc);
263
264 } else
265 pr_err("%s: Driver not enabled\n", __func__);
266 return rc;
267}
268
269/* ------------------- dsp --------------------- */
270static void audlpa_async_send_data(struct audio *audio, unsigned needed,
271 uint32_t token)
272{
273 unsigned long flags;
274 struct audio_client *ac;
275 int rc = 0;
276
277 pr_debug("%s:\n", __func__);
278 spin_lock_irqsave(&audio->dsp_lock, flags);
279
280 pr_debug("%s: needed = %d, out_needed = %d, token = 0x%x\n",
281 __func__, needed, audio->out_needed, token);
282 if (needed && !audio->wflush) {
283 audio->out_needed = 1;
284 if (audio->drv_status & ADRV_STATUS_OBUF_GIVEN) {
285 /* pop one node out of queue */
286 union msm_audio_event_payload evt_payload;
287 struct audlpa_buffer_node *used_buf;
288
289 used_buf = list_first_entry(&audio->out_queue,
290 struct audlpa_buffer_node, list);
291 if (token == used_buf->paddr) {
292 pr_debug("%s, Release: addr: %lx,"
293 " token = 0x%x\n", __func__,
294 used_buf->paddr, token);
295 list_del(&used_buf->list);
296 evt_payload.aio_buf = used_buf->buf;
297 audlpa_post_event(audio, AUDIO_EVENT_WRITE_DONE,
298 evt_payload);
299 kfree(used_buf);
300 audio->drv_status &= ~ADRV_STATUS_OBUF_GIVEN;
301 }
302 }
303 }
304 pr_debug("%s: out_needed = %d, stopped = %d, drv_status = 0x%x\n",
305 __func__, audio->out_needed, audio->stopped,
306 audio->drv_status);
307 if (audio->out_needed && (audio->stopped == 0)) {
308 struct audlpa_buffer_node *next_buf;
309 struct audio_aio_write_param param;
310 if (!list_empty(&audio->out_queue)) {
311 pr_debug("%s: list not empty\n", __func__);
312 next_buf = list_first_entry(&audio->out_queue,
313 struct audlpa_buffer_node, list);
314 if (next_buf) {
315 pr_debug("%s: Send: addr: %lx\n", __func__,
316 next_buf->paddr);
317 ac = audio->ac;
318 param.paddr = next_buf->paddr;
319 param.len = next_buf->buf.data_len;
320 param.msw_ts = 0;
321 param.lsw_ts = 0;
322 /* No time stamp valid */
323 param.flags = NO_TIMESTAMP;
324 param.uid = next_buf->paddr;
325 rc = q6asm_async_write(ac, &param);
326 if (rc < 0)
327 pr_err("%s:q6asm_async_write failed\n",
328 __func__);
329 audio->out_needed = 0;
330 audio->drv_status |= ADRV_STATUS_OBUF_GIVEN;
331 }
332 } else if (list_empty(&audio->out_queue) &&
333 (audio->drv_status & ADRV_STATUS_FSYNC)) {
334 pr_debug("%s: list is empty, reached EOS\n", __func__);
335 wake_up(&audio->write_wait);
336 }
337 }
338
339 spin_unlock_irqrestore(&audio->dsp_lock, flags);
340}
341
342static int audlpa_events_pending(struct audio *audio)
343{
344 int empty;
345
346 spin_lock(&audio->event_queue_lock);
347 empty = !list_empty(&audio->event_queue);
348 spin_unlock(&audio->event_queue_lock);
349 return empty || audio->event_abort;
350}
351
352static void audlpa_reset_event_queue(struct audio *audio)
353{
354 struct audlpa_event *drv_evt;
355 struct list_head *ptr, *next;
356
357 spin_lock(&audio->event_queue_lock);
358 list_for_each_safe(ptr, next, &audio->event_queue) {
359 drv_evt = list_first_entry(&audio->event_queue,
360 struct audlpa_event, list);
361 list_del(&drv_evt->list);
362 kfree(drv_evt);
363 }
364 list_for_each_safe(ptr, next, &audio->free_event_queue) {
365 drv_evt = list_first_entry(&audio->free_event_queue,
366 struct audlpa_event, list);
367 list_del(&drv_evt->list);
368 kfree(drv_evt);
369 }
370 spin_unlock(&audio->event_queue_lock);
371
372 return;
373}
374
375static long audlpa_process_event_req(struct audio *audio, void __user *arg)
376{
377 long rc;
378 struct msm_audio_event usr_evt;
379 struct audlpa_event *drv_evt = NULL;
380 int timeout;
381
382 if (copy_from_user(&usr_evt, arg, sizeof(struct msm_audio_event)))
383 return -EFAULT;
384
385 timeout = (int) usr_evt.timeout_ms;
386
387 if (timeout > 0) {
388 rc = wait_event_interruptible_timeout(
389 audio->event_wait, audlpa_events_pending(audio),
390 msecs_to_jiffies(timeout));
391 if (rc == 0)
392 return -ETIMEDOUT;
393 } else {
394 rc = wait_event_interruptible(
395 audio->event_wait, audlpa_events_pending(audio));
396 }
397
398 if (rc < 0)
399 return rc;
400
401 if (audio->event_abort) {
402 audio->event_abort = 0;
403 return -ENODEV;
404 }
405
406 rc = 0;
407
408 spin_lock(&audio->event_queue_lock);
409 if (!list_empty(&audio->event_queue)) {
410 drv_evt = list_first_entry(&audio->event_queue,
411 struct audlpa_event, list);
412 list_del(&drv_evt->list);
413 }
414 if (drv_evt) {
415 usr_evt.event_type = drv_evt->event_type;
416 usr_evt.event_payload = drv_evt->payload;
417 list_add_tail(&drv_evt->list, &audio->free_event_queue);
418 } else
419 rc = -1;
420 spin_unlock(&audio->event_queue_lock);
421
Vasudeva Rao Thumati86edf6c2011-07-06 16:25:13 +0530422 if (drv_evt && (drv_evt->event_type == AUDIO_EVENT_WRITE_DONE ||
423 drv_evt->event_type == AUDIO_EVENT_READ_DONE)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700424 pr_debug("%s: AUDIO_EVENT_WRITE_DONE completing\n", __func__);
425 mutex_lock(&audio->lock);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530426 audlpa_ion_fixup(audio, drv_evt->payload.aio_buf.buf_addr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427 drv_evt->payload.aio_buf.buf_len, 0);
428 mutex_unlock(&audio->lock);
429 }
430 if (!rc && copy_to_user(arg, &usr_evt, sizeof(usr_evt)))
431 rc = -EFAULT;
432
433 return rc;
434}
435
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530436static int audlpa_ion_check(struct audio *audio,
437 void *vaddr, unsigned long len)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700438{
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530439 struct audlpa_ion_region *region_elt;
440 struct audlpa_ion_region t = {.vaddr = vaddr, .len = len };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700441
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530442 list_for_each_entry(region_elt, &audio->ion_region_queue, list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700443 if (CONTAINS(region_elt, &t) || CONTAINS(&t, region_elt) ||
444 OVERLAPS(region_elt, &t)) {
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530445 pr_err("%s[%p]:region (vaddr %p len %ld)"
446 " clashes with registered region"
447 " (vaddr %p paddr %p len %ld)\n",
448 __func__, audio, vaddr, len,
449 region_elt->vaddr,
450 (void *)region_elt->paddr, region_elt->len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700451 return -EINVAL;
452 }
453 }
454
455 return 0;
456}
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530457static int audlpa_ion_add(struct audio *audio,
458 struct msm_audio_ion_info *info)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459{
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530460 ion_phys_addr_t paddr;
461 size_t len;
462 unsigned long kvaddr;
463 struct audlpa_ion_region *region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464 int rc = -EINVAL;
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530465 struct ion_handle *handle;
466 struct ion_client *client;
467 unsigned long ionflag;
468 void *temp_ptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700469
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530470 pr_debug("%s[%p]:\n", __func__, audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700471 region = kmalloc(sizeof(*region), GFP_KERNEL);
472
473 if (!region) {
474 rc = -ENOMEM;
475 goto end;
476 }
477
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530478 client = msm_ion_client_create(UINT_MAX, "Audio_LPA_Client");
479 if (IS_ERR_OR_NULL(client)) {
480 pr_err("Unable to create ION client\n");
481 goto client_error;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700482 }
483
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530484 handle = ion_import_fd(client, info->fd);
485 if (IS_ERR_OR_NULL(handle)) {
486 pr_err("%s: could not get handle of the given fd\n", __func__);
487 goto import_error;
488 }
489
490 rc = ion_handle_get_flags(client, handle, &ionflag);
491 if (rc) {
492 pr_err("%s: could not get flags for the handle\n", __func__);
493 goto flag_error;
494 }
495
496 temp_ptr = ion_map_kernel(client, handle, ionflag);
497 if (IS_ERR_OR_NULL(temp_ptr)) {
498 pr_err("%s: could not get virtual address\n", __func__);
499 goto map_error;
500 }
501 kvaddr = (unsigned long) temp_ptr;
502
503 rc = ion_phys(client, handle, &paddr, &len);
504 if (rc) {
505 pr_err("%s: could not get physical address\n", __func__);
506 goto ion_error;
507 }
508
509 rc = audlpa_ion_check(audio, info->vaddr, len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510 if (rc < 0) {
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530511 pr_err("%s: audlpa_ion_check failed\n", __func__);
512 goto ion_error;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513 }
514
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530515 region->client = client;
516 region->handle = handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517 region->vaddr = info->vaddr;
518 region->fd = info->fd;
519 region->paddr = paddr;
520 region->kvaddr = kvaddr;
521 region->len = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522 region->ref_cnt = 0;
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530523 pr_debug("%s[%p]:add region paddr %lx vaddr %p, len %lu kvaddr %lx\n",
524 __func__, audio,
525 region->paddr, region->vaddr, region->len, region->kvaddr);
526 list_add_tail(&region->list, &audio->ion_region_queue);
527
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528 rc = q6asm_memory_map(audio->ac, (uint32_t)paddr, IN, (uint32_t)len, 1);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530529 if (rc < 0) {
530 pr_err("%s[%p]: memory map failed\n", __func__, audio);
531 goto ion_error;
532 } else {
533 goto end;
534 }
535
536ion_error:
537 ion_unmap_kernel(client, handle);
538map_error:
539 ion_free(client, handle);
540flag_error:
541import_error:
542 ion_client_destroy(client);
543client_error:
544 kfree(region);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545end:
546 return rc;
547}
548
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530549static int audlpa_ion_remove(struct audio *audio,
550 struct msm_audio_ion_info *info)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551{
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530552 struct audlpa_ion_region *region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700553 struct list_head *ptr, *next;
554 int rc = -EINVAL;
555
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530556 list_for_each_safe(ptr, next, &audio->ion_region_queue) {
557 region = list_entry(ptr, struct audlpa_ion_region, list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530559 if (region != NULL && (region->fd == info->fd) &&
560 (region->vaddr == info->vaddr)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561 if (region->ref_cnt) {
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530562 pr_debug("%s[%p]:region %p in use ref_cnt %d\n",
563 __func__, audio, region,
564 region->ref_cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565 break;
566 }
567 rc = q6asm_memory_unmap(audio->ac,
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530568 (uint32_t) region->paddr, IN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569 if (rc < 0)
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530570 pr_err("%s[%p]: memory unmap failed\n",
571 __func__, audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572
573 list_del(&region->list);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530574 ion_unmap_kernel(region->client, region->handle);
575 ion_free(region->client, region->handle);
576 ion_client_destroy(region->client);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577 kfree(region);
578 rc = 0;
579 break;
580 }
581 }
582
583 return rc;
584}
585
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530586static int audlpa_ion_lookup_vaddr(struct audio *audio, void *addr,
587 unsigned long len, struct audlpa_ion_region **region)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700588{
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530589 struct audlpa_ion_region *region_elt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590 int match_count = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591 *region = NULL;
592
593 /* returns physical address or zero */
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530594 list_for_each_entry(region_elt, &audio->ion_region_queue, list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595 if (addr >= region_elt->vaddr &&
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530596 addr < region_elt->vaddr + region_elt->len &&
597 addr + len <= region_elt->vaddr + region_elt->len) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700598 /* offset since we could pass vaddr inside a registerd
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530599 * ion buffer
600 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601
602 match_count++;
603 if (!*region)
604 *region = region_elt;
605 }
606 }
607
608 if (match_count > 1) {
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530609 pr_err("%s[%p]:multiple hits for vaddr %p, len %ld\n",
610 __func__, audio, addr, len);
611 list_for_each_entry(region_elt, &audio->ion_region_queue,
612 list) {
613 if (addr >= region_elt->vaddr &&
614 addr < region_elt->vaddr + region_elt->len &&
615 addr + len <= region_elt->vaddr + region_elt->len)
616 pr_err("\t%s[%p]:%p, %ld --> %p\n",
617 __func__, audio,
618 region_elt->vaddr,
619 region_elt->len,
620 (void *)region_elt->paddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700621 }
622 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623 return *region ? 0 : -1;
624}
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530625static unsigned long audlpa_ion_fixup(struct audio *audio, void *addr,
626 unsigned long len, int ref_up)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627{
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530628 struct audlpa_ion_region *region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700629 unsigned long paddr;
630 int ret;
631
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530632 ret = audlpa_ion_lookup_vaddr(audio, addr, len, &region);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700633 if (ret) {
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530634 pr_err("%s[%p]:lookup (%p, %ld) failed\n",
635 __func__, audio, addr, len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700636 return 0;
637 }
638 if (ref_up)
639 region->ref_cnt++;
640 else
641 region->ref_cnt--;
642 paddr = region->paddr + (addr - region->vaddr);
643 return paddr;
644}
645
646/* audio -> lock must be held at this point */
647static int audlpa_aio_buf_add(struct audio *audio, unsigned dir,
648 void __user *arg)
649{
650 struct audlpa_buffer_node *buf_node;
651
652 buf_node = kmalloc(sizeof(*buf_node), GFP_KERNEL);
653
654 if (!buf_node)
655 return -ENOMEM;
656
657 if (copy_from_user(&buf_node->buf, arg, sizeof(buf_node->buf))) {
658 kfree(buf_node);
659 return -EFAULT;
660 }
661
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530662 buf_node->paddr = audlpa_ion_fixup(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700663 audio, buf_node->buf.buf_addr,
664 buf_node->buf.buf_len, 1);
665 if (dir) {
666 /* write */
667 if (!buf_node->paddr ||
668 (buf_node->paddr & 0x1) ||
669 (buf_node->buf.data_len & 0x1)) {
670 kfree(buf_node);
671 return -EINVAL;
672 }
673 list_add_tail(&buf_node->list, &audio->out_queue);
674 pr_debug("%s, Added to list: addr: %lx, length = %d\n",
675 __func__, buf_node->paddr, buf_node->buf.data_len);
676 audlpa_async_send_data(audio, 0, 0);
677 } else {
678 /* read */
Vasudeva Rao Thumati86edf6c2011-07-06 16:25:13 +0530679 kfree(buf_node);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700680 }
681 return 0;
682}
683
684static int config(struct audio *audio)
685{
686 int rc = 0;
687 if (!audio->out_prefill) {
688 if (audio->codec_ops.set_params != NULL) {
689 rc = audio->codec_ops.set_params(audio);
690 audio->out_prefill = 1;
691 }
692 }
693 return rc;
694}
695
696void q6_audlpa_out_cb(uint32_t opcode, uint32_t token,
697 uint32_t *payload, void *priv)
698{
699 struct audio *audio = (struct audio *) priv;
700
701 switch (opcode) {
702 case ASM_DATA_EVENT_WRITE_DONE:
703 pr_debug("%s: ASM_DATA_EVENT_WRITE_DONE, token = 0x%x\n",
704 __func__, token);
705 audlpa_async_send_data(audio, 1, token);
706 break;
707 case ASM_DATA_EVENT_EOS:
708 case ASM_DATA_CMDRSP_EOS:
709 pr_debug("%s: ASM_DATA_CMDRSP_EOS, teos = %d\n", __func__,
710 audio->teos);
711 if (audio->teos == 0) {
712 audio->teos = 1;
713 wake_up(&audio->write_wait);
714 }
715 break;
716 case ASM_SESSION_CMDRSP_GET_SESSION_TIME:
717 break;
Laxminath Kasam692c6542012-02-21 11:17:47 +0530718 case RESET_EVENTS:
719 reset_device();
720 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700721 default:
722 break;
723 }
724}
725
726static long pcm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
727{
728 pr_debug("%s: cmd = %d\n", __func__, cmd);
729 return -EINVAL;
730}
731
732static int audlpa_set_pcm_params(void *data)
733{
734 struct audio *audio = (struct audio *)data;
735 int rc;
736
737 rc = q6asm_media_format_block_pcm(audio->ac, audio->out_sample_rate,
738 audio->out_channel_mode);
739 if (rc < 0)
740 pr_err("%s: Format block pcm failed\n", __func__);
741 return rc;
742}
743
744static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
745{
746 struct audio *audio = file->private_data;
747 int rc = -EINVAL;
748 uint64_t timestamp;
749 uint64_t temp;
750
751 pr_debug("%s: audio_ioctl() cmd = %d\n", __func__, cmd);
752
753 if (cmd == AUDIO_GET_STATS) {
754 struct msm_audio_stats stats;
755
756 pr_debug("%s: AUDIO_GET_STATS cmd\n", __func__);
757 memset(&stats, 0, sizeof(stats));
758 timestamp = q6asm_get_session_time(audio->ac);
759 if (timestamp < 0) {
760 pr_err("%s: Get Session Time return value =%lld\n",
761 __func__, timestamp);
762 return -EAGAIN;
763 }
764 temp = (timestamp * 2 * audio->out_channel_mode);
765 temp = temp * (audio->out_sample_rate/1000);
766 temp = div_u64(temp, 1000);
767 audio->bytes_consumed = (uint32_t)(temp & 0xFFFFFFFF);
768 stats.byte_count = audio->bytes_consumed;
769 stats.unused[0] = (uint32_t)((temp >> 32) & 0xFFFFFFFF);
770 pr_debug("%s: bytes_consumed:lsb = %d, msb = %d,"
771 "timestamp = %lld\n", __func__,
772 audio->bytes_consumed, stats.unused[0], timestamp);
773 if (copy_to_user((void *) arg, &stats, sizeof(stats)))
774 return -EFAULT;
775 return 0;
776 }
777
778 switch (cmd) {
779 case AUDIO_ENABLE_AUDPP:
780 break;
781
782 case AUDIO_SET_VOLUME:
783 break;
784
785 case AUDIO_SET_PAN:
786 break;
787
788 case AUDIO_SET_EQ:
789 break;
790 }
791
792 if (cmd == AUDIO_GET_EVENT) {
793 pr_debug("%s: AUDIO_GET_EVENT\n", __func__);
794 if (mutex_trylock(&audio->get_event_lock)) {
795 rc = audlpa_process_event_req(audio,
796 (void __user *) arg);
797 mutex_unlock(&audio->get_event_lock);
798 } else
799 rc = -EBUSY;
800 return rc;
801 }
802
803 if (cmd == AUDIO_ABORT_GET_EVENT) {
804 audio->event_abort = 1;
805 wake_up(&audio->event_wait);
806 return 0;
807 }
808
809 mutex_lock(&audio->lock);
810 switch (cmd) {
811 case AUDIO_START:
812 pr_info("%s: AUDIO_START: Session %d\n", __func__,
813 audio->ac->session);
814 if (!audio->opened) {
815 pr_err("%s: Driver not opened\n", __func__);
816 rc = -EFAULT;
817 goto fail;
818 }
819 rc = config(audio);
820 if (rc) {
821 pr_err("%s: Out Configuration failed\n", __func__);
822 rc = -EFAULT;
823 goto fail;
824 }
825
826 rc = audio_enable(audio);
827 if (rc) {
828 pr_err("%s: audio enable failed\n", __func__);
829 rc = -EFAULT;
830 goto fail;
831 } else {
Swaminathan Sathappanb0021cd2011-08-31 15:20:12 -0700832 struct asm_softpause_params softpause = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700833 .enable = SOFT_PAUSE_ENABLE,
834 .period = SOFT_PAUSE_PERIOD,
835 .step = SOFT_PAUSE_STEP,
836 .rampingcurve = SOFT_PAUSE_CURVE_LINEAR,
837 };
Swaminathan Sathappanb0021cd2011-08-31 15:20:12 -0700838 struct asm_softvolume_params softvol = {
839 .period = SOFT_VOLUME_PERIOD,
840 .step = SOFT_VOLUME_STEP,
841 .rampingcurve = SOFT_VOLUME_CURVE_LINEAR,
842 };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843 audio->out_enabled = 1;
844 audio->out_needed = 1;
845 rc = q6asm_set_volume(audio->ac, audio->volume);
846 if (rc < 0)
847 pr_err("%s: Send Volume command failed rc=%d\n",
848 __func__, rc);
Swaminathan Sathappanb0021cd2011-08-31 15:20:12 -0700849 rc = q6asm_set_softpause(audio->ac, &softpause);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700850 if (rc < 0)
851 pr_err("%s: Send SoftPause Param failed rc=%d\n",
852 __func__, rc);
Swaminathan Sathappanb0021cd2011-08-31 15:20:12 -0700853 rc = q6asm_set_softvolume(audio->ac, &softvol);
854 if (rc < 0)
855 pr_err("%s: Send SoftVolume Param failed rc=%d\n",
856 __func__, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700857 rc = q6asm_set_lrgain(audio->ac, 0x2000, 0x2000);
858 if (rc < 0)
859 pr_err("%s: Send channel gain failed rc=%d\n",
860 __func__, rc);
861 /* disable mute by default */
862 rc = q6asm_set_mute(audio->ac, 0);
863 if (rc < 0)
864 pr_err("%s: Send mute command failed rc=%d\n",
865 __func__, rc);
866 if (!list_empty(&audio->out_queue))
867 pr_err("%s: write_list is not empty!!!\n",
868 __func__);
869 if (audio->stopped == 1)
870 audio->stopped = 0;
871 audlpa_prevent_sleep(audio);
872 }
873 break;
874
875 case AUDIO_STOP:
876 pr_info("%s: AUDIO_STOP: session_id:%d\n", __func__,
877 audio->ac->session);
878 audio->stopped = 1;
879 audlpa_async_flush(audio);
880 audio->out_enabled = 0;
881 audio->out_needed = 0;
882 audio->drv_status &= ~ADRV_STATUS_PAUSE;
883 audlpa_allow_sleep(audio);
884 break;
885
886 case AUDIO_FLUSH:
887 pr_debug("%s: AUDIO_FLUSH: session_id:%d\n", __func__,
888 audio->ac->session);
889 audio->wflush = 1;
890 if (audio->out_enabled)
891 audlpa_async_flush(audio);
892 else
893 audio->wflush = 0;
894 audio->wflush = 0;
895 break;
896
897 case AUDIO_SET_CONFIG:{
898 struct msm_audio_config config;
899 pr_debug("%s: AUDIO_SET_CONFIG\n", __func__);
900 if (copy_from_user(&config, (void *) arg, sizeof(config))) {
901 rc = -EFAULT;
902 pr_err("%s: ERROR: copy from user\n", __func__);
903 break;
904 }
905 if (!((config.channel_count == 1) ||
906 (config.channel_count == 2))) {
907 rc = -EINVAL;
908 pr_err("%s: ERROR: config.channel_count == %d\n",
909 __func__, config.channel_count);
910 break;
911 }
912
913 if (!((config.bits == 8) || (config.bits == 16) ||
914 (config.bits == 24))) {
915 rc = -EINVAL;
916 pr_err("%s: ERROR: config.bits = %d\n", __func__,
917 config.bits);
918 break;
919 }
920 audio->out_sample_rate = config.sample_rate;
921 audio->out_channel_mode = config.channel_count;
922 audio->out_bits = config.bits;
923 audio->buffer_count = config.buffer_count;
924 audio->buffer_size = config.buffer_size;
925 rc = 0;
926 break;
927 }
928
929 case AUDIO_GET_CONFIG:{
930 struct msm_audio_config config;
931 config.buffer_count = audio->buffer_count;
932 config.buffer_size = audio->buffer_size;
933 config.sample_rate = audio->out_sample_rate;
934 config.channel_count = audio->out_channel_mode;
935 config.bits = audio->out_bits;
936
937 config.meta_field = 0;
938 config.unused[0] = 0;
939 config.unused[1] = 0;
940 config.unused[2] = 0;
941 if (copy_to_user((void *) arg, &config, sizeof(config)))
942 rc = -EFAULT;
943 else
944 rc = 0;
945 break;
946 }
947
948 case AUDIO_PAUSE:
949 pr_debug("%s: AUDIO_PAUSE %ld\n", __func__, arg);
950 if (arg == 1) {
951 rc = audlpa_pause(audio);
952 if (rc < 0)
953 pr_err("%s: pause FAILED rc=%d\n", __func__,
954 rc);
955 audio->drv_status |= ADRV_STATUS_PAUSE;
956 } else if (arg == 0) {
957 if (audio->drv_status & ADRV_STATUS_PAUSE) {
958 rc = audio_enable(audio);
959 if (rc)
960 pr_err("%s: audio enable failed\n",
961 __func__);
962 else {
963 audio->drv_status &= ~ADRV_STATUS_PAUSE;
964 audio->out_enabled = 1;
965 }
966 }
967 }
968 break;
969
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530970 case AUDIO_REGISTER_ION: {
971 struct msm_audio_ion_info info;
972 pr_debug("%s: AUDIO_REGISTER_ION\n", __func__);
973 if (copy_from_user(&info, (void *)arg, sizeof(info)))
974 rc = -EFAULT;
975 else
976 rc = audlpa_ion_add(audio, &info);
977 break;
978 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700979
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530980 case AUDIO_DEREGISTER_ION: {
981 struct msm_audio_ion_info info;
982 pr_debug("%s: AUDIO_DEREGISTER_ION\n", __func__);
983 if (copy_from_user(&info, (void *)arg, sizeof(info)))
984 rc = -EFAULT;
985 else
986 rc = audlpa_ion_remove(audio, &info);
987 break;
988 }
989
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700990 case AUDIO_ASYNC_WRITE:
991 pr_debug("%s: AUDIO_ASYNC_WRITE\n", __func__);
992 if (audio->drv_status & ADRV_STATUS_FSYNC)
993 rc = -EBUSY;
994 else
995 rc = audlpa_aio_buf_add(audio, 1, (void __user *) arg);
996 break;
997
998 case AUDIO_GET_SESSION_ID:
999 if (copy_to_user((void *) arg, &audio->ac->session,
1000 sizeof(unsigned short)))
1001 return -EFAULT;
1002 rc = 0;
1003 break;
1004
1005 default:
1006 rc = audio->codec_ops.ioctl(file, cmd, arg);
1007 }
1008fail:
1009 mutex_unlock(&audio->lock);
1010 return rc;
1011}
1012
1013/* Only useful in tunnel-mode */
1014int audlpa_async_fsync(struct audio *audio)
1015{
1016 int rc = 0;
1017
1018 pr_info("%s:Session %d\n", __func__, audio->ac->session);
1019
1020 /* Blocking client sends more data */
1021 mutex_lock(&audio->lock);
1022 audio->drv_status |= ADRV_STATUS_FSYNC;
1023 mutex_unlock(&audio->lock);
1024
1025 mutex_lock(&audio->write_lock);
1026 audio->teos = 0;
1027
1028 rc = wait_event_interruptible(audio->write_wait,
1029 ((list_empty(&audio->out_queue)) ||
1030 audio->wflush || audio->stopped));
1031
1032 if (audio->wflush || audio->stopped)
1033 goto flush_event;
1034
1035 if (rc < 0) {
1036 pr_err("%s: wait event for list_empty failed, rc = %d\n",
1037 __func__, rc);
1038 goto done;
1039 }
1040
1041 rc = q6asm_cmd(audio->ac, CMD_EOS);
1042
1043 if (rc < 0) {
1044 pr_err("%s: q6asm_cmd failed, rc = %d", __func__, rc);
1045 goto done;
1046 }
1047 rc = wait_event_interruptible_timeout(audio->write_wait,
1048 (audio->teos || audio->wflush ||
1049 audio->stopped), 5*HZ);
1050
1051 if (rc < 0) {
1052 pr_err("%s: wait event for teos failed, rc = %d\n", __func__,
1053 rc);
1054 goto done;
1055 }
1056
1057 if (audio->teos == 1) {
1058 rc = audio_enable(audio);
1059 if (rc)
1060 pr_err("%s: audio enable failed\n", __func__);
1061 else {
1062 audio->drv_status &= ~ADRV_STATUS_PAUSE;
1063 audio->out_enabled = 1;
1064 audio->out_needed = 1;
1065 }
1066 }
1067
1068flush_event:
1069 if (audio->stopped || audio->wflush)
1070 rc = -EBUSY;
1071
1072done:
1073 mutex_unlock(&audio->write_lock);
1074 mutex_lock(&audio->lock);
1075 audio->drv_status &= ~ADRV_STATUS_FSYNC;
1076 mutex_unlock(&audio->lock);
1077
1078 return rc;
1079}
1080
1081int audlpa_fsync(struct file *file, int datasync)
1082{
1083 struct audio *audio = file->private_data;
1084
1085 return audlpa_async_fsync(audio);
1086}
1087
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301088void audlpa_reset_ion_region(struct audio *audio)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001089{
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301090 struct audlpa_ion_region *region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001091 struct list_head *ptr, *next;
1092
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301093 list_for_each_safe(ptr, next, &audio->ion_region_queue) {
1094 region = list_entry(ptr, struct audlpa_ion_region, list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001095 list_del(&region->list);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301096 ion_unmap_kernel(region->client, region->handle);
1097 ion_free(region->client, region->handle);
1098 ion_client_destroy(region->client);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001099 kfree(region);
1100 }
1101
1102 return;
1103}
1104
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301105static void audlpa_unmap_ion_region(struct audio *audio)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001106{
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301107 struct audlpa_ion_region *region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001108 struct list_head *ptr, *next;
1109 int rc = -EINVAL;
1110
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301111 pr_debug("%s[%p]:\n", __func__, audio);
1112 list_for_each_safe(ptr, next, &audio->ion_region_queue) {
1113 region = list_entry(ptr, struct audlpa_ion_region, list);
1114 pr_debug("%s[%p]: phy_address = 0x%lx\n",
1115 __func__, audio, region->paddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001116 if (region != NULL) {
1117 rc = q6asm_memory_unmap(audio->ac,
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301118 (uint32_t)region->paddr, IN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001119 if (rc < 0)
1120 pr_err("%s: memory unmap failed\n", __func__);
1121 }
1122 }
1123}
1124
1125static int audio_release(struct inode *inode, struct file *file)
1126{
1127 struct audio *audio = file->private_data;
1128
1129 pr_info("%s: audio instance 0x%08x freeing, session %d\n", __func__,
1130 (int)audio, audio->ac->session);
1131
1132 mutex_lock(&audio->lock);
1133 audio->wflush = 1;
1134 if (audio->out_enabled)
1135 audlpa_async_flush(audio);
1136 audio->wflush = 0;
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301137 audlpa_unmap_ion_region(audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001138 audio_disable(audio);
1139 msm_clear_session_id(audio->ac->session);
1140 auddev_unregister_evt_listner(AUDDEV_CLNT_DEC, audio->ac->session);
1141 q6asm_audio_client_free(audio->ac);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301142 audlpa_reset_ion_region(audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001143#ifdef CONFIG_HAS_EARLYSUSPEND
1144 unregister_early_suspend(&audio->suspend_ctl.node);
1145#endif
1146 audio->opened = 0;
1147 audio->out_enabled = 0;
1148 audio->out_prefill = 0;
1149 audio->event_abort = 1;
1150 wake_up(&audio->event_wait);
1151 audlpa_reset_event_queue(audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001152 if (audio->stopped == 0)
1153 audlpa_allow_sleep(audio);
1154 wake_lock_destroy(&audio->wakelock);
1155
1156 mutex_unlock(&audio->lock);
1157#ifdef CONFIG_DEBUG_FS
1158 if (audio->dentry)
1159 debugfs_remove(audio->dentry);
1160#endif
1161 kfree(audio);
1162 return 0;
1163}
1164
1165static void audlpa_post_event(struct audio *audio, int type,
1166 union msm_audio_event_payload payload)
1167{
1168 struct audlpa_event *e_node = NULL;
1169
1170 spin_lock(&audio->event_queue_lock);
1171
1172 pr_debug("%s:\n", __func__);
1173 if (!list_empty(&audio->free_event_queue)) {
1174 e_node = list_first_entry(&audio->free_event_queue,
1175 struct audlpa_event, list);
1176 list_del(&e_node->list);
1177 } else {
1178 e_node = kmalloc(sizeof(struct audlpa_event), GFP_ATOMIC);
1179 if (!e_node) {
1180 pr_err("%s: No mem to post event %d\n", __func__, type);
1181 return;
1182 }
1183 }
1184
1185 e_node->event_type = type;
1186 e_node->payload = payload;
1187
1188 list_add_tail(&e_node->list, &audio->event_queue);
1189 spin_unlock(&audio->event_queue_lock);
1190 wake_up(&audio->event_wait);
1191}
1192
1193#ifdef CONFIG_HAS_EARLYSUSPEND
1194static void audlpa_suspend(struct early_suspend *h)
1195{
1196 struct audlpa_suspend_ctl *ctl =
1197 container_of(h, struct audlpa_suspend_ctl, node);
1198 union msm_audio_event_payload payload;
1199
1200 pr_debug("%s:\n", __func__);
1201 audlpa_post_event(ctl->audio, AUDIO_EVENT_SUSPEND, payload);
1202}
1203
1204static void audlpa_resume(struct early_suspend *h)
1205{
1206 struct audlpa_suspend_ctl *ctl =
1207 container_of(h, struct audlpa_suspend_ctl, node);
1208 union msm_audio_event_payload payload;
1209
1210 pr_debug("%s:\n", __func__);
1211 audlpa_post_event(ctl->audio, AUDIO_EVENT_RESUME, payload);
1212}
1213#endif
1214
1215#ifdef CONFIG_DEBUG_FS
1216static ssize_t audlpa_debug_open(struct inode *inode, struct file *file)
1217{
1218 file->private_data = inode->i_private;
1219 return 0;
1220}
1221
1222static ssize_t audlpa_debug_read(struct file *file, char __user *buf,
1223 size_t count, loff_t *ppos)
1224{
1225 const int debug_bufmax = 4096;
1226 static char buffer[4096];
1227 int n = 0;
1228 struct audio *audio = file->private_data;
1229
1230 mutex_lock(&audio->lock);
1231 n = scnprintf(buffer, debug_bufmax, "opened %d\n", audio->opened);
1232 n += scnprintf(buffer + n, debug_bufmax - n,
1233 "out_enabled %d\n", audio->out_enabled);
1234 n += scnprintf(buffer + n, debug_bufmax - n,
1235 "stopped %d\n", audio->stopped);
1236 n += scnprintf(buffer + n, debug_bufmax - n,
1237 "volume %x\n", audio->volume);
1238 n += scnprintf(buffer + n, debug_bufmax - n,
1239 "sample rate %d\n",
1240 audio->out_sample_rate);
1241 n += scnprintf(buffer + n, debug_bufmax - n,
1242 "channel mode %d\n",
1243 audio->out_channel_mode);
1244 mutex_unlock(&audio->lock);
1245 /* Following variables are only useful for debugging when
1246 * when playback halts unexpectedly. Thus, no mutual exclusion
1247 * enforced
1248 */
1249 n += scnprintf(buffer + n, debug_bufmax - n,
1250 "wflush %d\n", audio->wflush);
1251 n += scnprintf(buffer + n, debug_bufmax - n,
1252 "running %d\n", audio->running);
1253 n += scnprintf(buffer + n, debug_bufmax - n,
1254 "out_needed %d\n", audio->out_needed);
1255 buffer[n] = 0;
1256 return simple_read_from_buffer(buf, count, ppos, buffer, n);
1257}
1258
1259static const struct file_operations audlpa_debug_fops = {
1260 .read = audlpa_debug_read,
1261 .open = audlpa_debug_open,
1262};
1263#endif
1264
1265static int audio_open(struct inode *inode, struct file *file)
1266{
1267 struct audio *audio = NULL;
1268 int rc, i, dec_attrb = 0;
1269 struct audlpa_event *e_node = NULL;
1270#ifdef CONFIG_DEBUG_FS
1271 /* 4 bytes represents decoder number, 1 byte for terminate string */
1272 char name[sizeof "msm_lpa_" + 5];
1273#endif
1274 char wake_lock_name[24];
1275
1276 /* Allocate audio instance, set to zero */
1277 audio = kzalloc(sizeof(struct audio), GFP_KERNEL);
1278 if (!audio) {
1279 pr_err("%s: no memory to allocate audio instance\n", __func__);
1280 rc = -ENOMEM;
1281 goto done;
1282 }
1283
1284 if ((file->f_mode & FMODE_WRITE) && !(file->f_mode & FMODE_READ)) {
1285 pr_debug("%s: Tunnel Mode playback\n", __func__);
1286 } else {
1287 kfree(audio);
1288 rc = -EACCES;
1289 goto done;
1290 }
1291
1292 /* Allocate the decoder based on inode minor number*/
1293 audio->minor_no = iminor(inode);
1294 dec_attrb |= audlpa_decs[audio->minor_no].dec_attrb;
1295 audio->codec_ops.ioctl = audlpa_decs[audio->minor_no].ioctl;
1296 audio->codec_ops.set_params = audlpa_decs[audio->minor_no].set_params;
1297 audio->buffer_size = BUFSZ;
1298 audio->buffer_count = MAX_BUF;
1299
1300 audio->ac = q6asm_audio_client_alloc((app_cb)q6_audlpa_out_cb,
1301 (void *)audio);
1302 if (!audio->ac) {
1303 pr_err("%s: Could not allocate memory for lpa client\n",
1304 __func__);
1305 rc = -ENOMEM;
1306 goto err;
1307 }
1308 rc = q6asm_open_write(audio->ac, FORMAT_LINEAR_PCM);
1309 if (rc < 0) {
1310 pr_err("%s: lpa out open failed\n", __func__);
1311 goto err;
1312 }
1313
1314 pr_debug("%s: Set mode to AIO session[%d]\n",
1315 __func__,
1316 audio->ac->session);
1317 rc = q6asm_set_io_mode(audio->ac, ASYNC_IO_MODE);
1318 if (rc < 0)
1319 pr_err("%s: Set IO mode failed\n", __func__);
1320
1321
1322 /* Initialize all locks of audio instance */
1323 mutex_init(&audio->lock);
1324 mutex_init(&audio->write_lock);
1325 mutex_init(&audio->get_event_lock);
1326 spin_lock_init(&audio->dsp_lock);
1327 init_waitqueue_head(&audio->write_wait);
1328 INIT_LIST_HEAD(&audio->out_queue);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301329 INIT_LIST_HEAD(&audio->ion_region_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001330 INIT_LIST_HEAD(&audio->free_event_queue);
1331 INIT_LIST_HEAD(&audio->event_queue);
1332 init_waitqueue_head(&audio->wait);
1333 init_waitqueue_head(&audio->event_wait);
1334 spin_lock_init(&audio->event_queue_lock);
1335 snprintf(wake_lock_name, sizeof wake_lock_name, "audio_lpa_%x",
1336 audio->ac->session);
1337 wake_lock_init(&audio->wakelock, WAKE_LOCK_SUSPEND, wake_lock_name);
1338
1339 audio->out_sample_rate = 44100;
1340 audio->out_channel_mode = 2;
1341 audio->out_bits = 16;
1342 audio->volume = 0x2000;
1343
1344 file->private_data = audio;
1345 audio->opened = 1;
1346 audio->out_enabled = 0;
1347 audio->out_prefill = 0;
1348 audio->bytes_consumed = 0;
1349
1350 audio->device_events = AUDDEV_EVT_STREAM_VOL_CHG;
1351 audio->drv_status &= ~ADRV_STATUS_PAUSE;
1352
1353 rc = auddev_register_evt_listner(audio->device_events,
1354 AUDDEV_CLNT_DEC,
1355 audio->ac->session,
1356 lpa_listner,
1357 (void *)audio);
1358 if (rc) {
1359 pr_err("%s: failed to register listner\n", __func__);
1360 goto err;
1361 }
1362
1363#ifdef CONFIG_DEBUG_FS
1364 snprintf(name, sizeof name, "msm_lpa_%04x", audio->ac->session);
1365 audio->dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
1366 NULL, (void *) audio, &audlpa_debug_fops);
1367
1368 if (IS_ERR(audio->dentry))
1369 pr_err("%s: debugfs_create_file failed\n", __func__);
1370#endif
1371#ifdef CONFIG_HAS_EARLYSUSPEND
1372 audio->suspend_ctl.node.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
1373 audio->suspend_ctl.node.resume = audlpa_resume;
1374 audio->suspend_ctl.node.suspend = audlpa_suspend;
1375 audio->suspend_ctl.audio = audio;
1376 register_early_suspend(&audio->suspend_ctl.node);
1377#endif
1378 for (i = 0; i < AUDLPA_EVENT_NUM; i++) {
1379 e_node = kmalloc(sizeof(struct audlpa_event), GFP_KERNEL);
1380 if (e_node)
1381 list_add_tail(&e_node->list, &audio->free_event_queue);
1382 else {
1383 pr_err("%s: event pkt alloc failed\n", __func__);
1384 break;
1385 }
1386 }
1387 pr_info("%s: audio instance 0x%08x created session[%d]\n", __func__,
1388 (int)audio,
1389 audio->ac->session);
1390done:
1391 return rc;
1392err:
1393 q6asm_audio_client_free(audio->ac);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001394 kfree(audio);
1395 return rc;
1396}
1397
1398static const struct file_operations audio_lpa_fops = {
1399 .owner = THIS_MODULE,
1400 .open = audio_open,
1401 .release = audio_release,
1402 .unlocked_ioctl = audio_ioctl,
1403 .fsync = audlpa_fsync,
1404};
1405
1406static dev_t audlpa_devno;
1407static struct class *audlpa_class;
1408struct audlpa_device {
1409 const char *name;
1410 struct device *device;
1411 struct cdev cdev;
1412};
1413
1414static struct audlpa_device *audlpa_devices;
1415
1416static void audlpa_create(struct audlpa_device *adev, const char *name,
1417 struct device *parent, dev_t devt)
1418{
1419 struct device *dev;
1420 int rc;
1421
1422 dev = device_create(audlpa_class, parent, devt, "%s", name);
1423 if (IS_ERR(dev))
1424 return;
1425
1426 cdev_init(&adev->cdev, &audio_lpa_fops);
1427 adev->cdev.owner = THIS_MODULE;
1428
1429 rc = cdev_add(&adev->cdev, devt, 1);
1430 if (rc < 0) {
1431 device_destroy(audlpa_class, devt);
1432 } else {
1433 adev->device = dev;
1434 adev->name = name;
1435 }
1436}
1437
1438static int __init audio_init(void)
1439{
1440 int rc;
1441 int n = ARRAY_SIZE(audlpa_decs);
1442
1443 audlpa_devices = kzalloc(sizeof(struct audlpa_device) * n, GFP_KERNEL);
1444 if (!audlpa_devices)
1445 return -ENOMEM;
1446
1447 audlpa_class = class_create(THIS_MODULE, "audlpa");
1448 if (IS_ERR(audlpa_class))
1449 goto fail_create_class;
1450
1451 rc = alloc_chrdev_region(&audlpa_devno, 0, n, "msm_audio_lpa");
1452 if (rc < 0)
1453 goto fail_alloc_region;
1454
1455 for (n = 0; n < ARRAY_SIZE(audlpa_decs); n++) {
1456 audlpa_create(audlpa_devices + n,
1457 audlpa_decs[n].name, NULL,
1458 MKDEV(MAJOR(audlpa_devno), n));
1459 }
1460
1461 return 0;
1462
1463fail_alloc_region:
1464 class_unregister(audlpa_class);
1465 return rc;
1466fail_create_class:
1467 kfree(audlpa_devices);
1468 return -ENOMEM;
1469}
1470
1471static void __exit audio_exit(void)
1472{
1473 class_unregister(audlpa_class);
1474 kfree(audlpa_devices);
1475}
1476
1477module_init(audio_init);
1478module_exit(audio_exit);
1479
1480MODULE_DESCRIPTION("MSM LPA driver");
1481MODULE_LICENSE("GPL v2");