blob: f89eb18aad694d7e015ef8ebc3100a85a2b1cc38 [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;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070094 int fd;
95 void *vaddr;
96 unsigned long paddr;
97 unsigned long kvaddr;
98 unsigned long len;
99 unsigned ref_cnt;
100};
101
102struct audlpa_buffer_node {
103 struct list_head list;
104 struct msm_audio_aio_buf buf;
105 unsigned long paddr;
106};
107
108struct audlpa_dec {
109 char *name;
110 int dec_attrb;
111 long (*ioctl)(struct file *, unsigned int, unsigned long);
112 int (*set_params)(void *);
113};
114
115static void audlpa_post_event(struct audio *audio, int type,
116 union msm_audio_event_payload payload);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530117
118static unsigned long audlpa_ion_fixup(struct audio *audio, void *addr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700119 unsigned long len, int ref_up);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530120static void audlpa_unmap_ion_region(struct audio *audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700121static void audlpa_async_send_data(struct audio *audio, unsigned needed,
122 uint32_t token);
123static int audlpa_pause(struct audio *audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700124static long pcm_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
125static int audlpa_set_pcm_params(void *data);
126
127struct audlpa_dec audlpa_decs[] = {
128 {"msm_pcm_lp_dec", AUDDEC_DEC_PCM, &pcm_ioctl,
129 &audlpa_set_pcm_params},
130};
131
132static 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
160static void audlpa_prevent_sleep(struct audio *audio)
161{
162 pr_debug("%s:\n", __func__);
163 wake_lock(&audio->wakelock);
164}
165
166static 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 */
173static 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
181static 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 */
232static 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}
252static 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 --------------------- */
269static 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, &param);
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
341static 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
351static 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
374static 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 Thumati86edf6c2011-07-06 16:25:13 +0530421 if (drv_evt && (drv_evt->event_type == AUDIO_EVENT_WRITE_DONE ||
422 drv_evt->event_type == AUDIO_EVENT_READ_DONE)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423 pr_debug("%s: AUDIO_EVENT_WRITE_DONE completing\n", __func__);
424 mutex_lock(&audio->lock);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530425 audlpa_ion_fixup(audio, drv_evt->payload.aio_buf.buf_addr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426 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
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530435static int audlpa_ion_check(struct audio *audio,
436 void *vaddr, unsigned long len)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700437{
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530438 struct audlpa_ion_region *region_elt;
439 struct audlpa_ion_region t = {.vaddr = vaddr, .len = len };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530441 list_for_each_entry(region_elt, &audio->ion_region_queue, list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700442 if (CONTAINS(region_elt, &t) || CONTAINS(&t, region_elt) ||
443 OVERLAPS(region_elt, &t)) {
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530444 pr_err("%s[%p]:region (vaddr %p len %ld)"
445 " clashes with registered region"
446 " (vaddr %p paddr %p len %ld)\n",
447 __func__, audio, vaddr, len,
448 region_elt->vaddr,
449 (void *)region_elt->paddr, region_elt->len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700450 return -EINVAL;
451 }
452 }
453
454 return 0;
455}
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530456static int audlpa_ion_add(struct audio *audio,
457 struct msm_audio_ion_info *info)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458{
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530459 ion_phys_addr_t paddr;
460 size_t len;
461 unsigned long kvaddr;
462 struct audlpa_ion_region *region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700463 int rc = -EINVAL;
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530464 struct ion_handle *handle;
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530465 unsigned long ionflag;
466 void *temp_ptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700467
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530468 pr_debug("%s[%p]:\n", __func__, audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700469 region = kmalloc(sizeof(*region), GFP_KERNEL);
470
471 if (!region) {
472 rc = -ENOMEM;
473 goto end;
474 }
475
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700476
Deepa Madiregamaa4795072012-06-15 22:35:52 +0530477 handle = ion_import_dma_buf(audio->client, info->fd);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530478 if (IS_ERR_OR_NULL(handle)) {
479 pr_err("%s: could not get handle of the given fd\n", __func__);
480 goto import_error;
481 }
482
Deepa Madiregamaa4795072012-06-15 22:35:52 +0530483 rc = ion_handle_get_flags(audio->client, handle, &ionflag);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530484 if (rc) {
485 pr_err("%s: could not get flags for the handle\n", __func__);
486 goto flag_error;
487 }
488
Deepa Madiregamaa4795072012-06-15 22:35:52 +0530489 temp_ptr = ion_map_kernel(audio->client, handle, ionflag);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530490 if (IS_ERR_OR_NULL(temp_ptr)) {
491 pr_err("%s: could not get virtual address\n", __func__);
492 goto map_error;
493 }
494 kvaddr = (unsigned long) temp_ptr;
495
Deepa Madiregamaa4795072012-06-15 22:35:52 +0530496 rc = ion_phys(audio->client, handle, &paddr, &len);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530497 if (rc) {
498 pr_err("%s: could not get physical address\n", __func__);
499 goto ion_error;
500 }
501
502 rc = audlpa_ion_check(audio, info->vaddr, len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700503 if (rc < 0) {
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530504 pr_err("%s: audlpa_ion_check failed\n", __func__);
505 goto ion_error;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506 }
507
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530508 region->handle = handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509 region->vaddr = info->vaddr;
510 region->fd = info->fd;
511 region->paddr = paddr;
512 region->kvaddr = kvaddr;
513 region->len = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514 region->ref_cnt = 0;
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530515 pr_debug("%s[%p]:add region paddr %lx vaddr %p, len %lu kvaddr %lx\n",
516 __func__, audio,
517 region->paddr, region->vaddr, region->len, region->kvaddr);
518 list_add_tail(&region->list, &audio->ion_region_queue);
519
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520 rc = q6asm_memory_map(audio->ac, (uint32_t)paddr, IN, (uint32_t)len, 1);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530521 if (rc < 0) {
522 pr_err("%s[%p]: memory map failed\n", __func__, audio);
523 goto ion_error;
524 } else {
525 goto end;
526 }
527
528ion_error:
Deepa Madiregamaa4795072012-06-15 22:35:52 +0530529 ion_unmap_kernel(audio->client, handle);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530530map_error:
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530531flag_error:
Deepa Madiregamaa4795072012-06-15 22:35:52 +0530532 ion_free(audio->client, handle);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530533import_error:
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530534 kfree(region);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535end:
536 return rc;
537}
538
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530539static int audlpa_ion_remove(struct audio *audio,
540 struct msm_audio_ion_info *info)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541{
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530542 struct audlpa_ion_region *region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543 struct list_head *ptr, *next;
544 int rc = -EINVAL;
545
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530546 list_for_each_safe(ptr, next, &audio->ion_region_queue) {
547 region = list_entry(ptr, struct audlpa_ion_region, list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530549 if (region != NULL && (region->fd == info->fd) &&
550 (region->vaddr == info->vaddr)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551 if (region->ref_cnt) {
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530552 pr_debug("%s[%p]:region %p in use ref_cnt %d\n",
553 __func__, audio, region,
554 region->ref_cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555 break;
556 }
557 rc = q6asm_memory_unmap(audio->ac,
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530558 (uint32_t) region->paddr, IN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559 if (rc < 0)
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530560 pr_err("%s[%p]: memory unmap failed\n",
561 __func__, audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562
563 list_del(&region->list);
Deepa Madiregamaa4795072012-06-15 22:35:52 +0530564 ion_unmap_kernel(audio->client, region->handle);
565 ion_free(audio->client, region->handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566 kfree(region);
567 rc = 0;
568 break;
569 }
570 }
571
572 return rc;
573}
574
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530575static int audlpa_ion_lookup_vaddr(struct audio *audio, void *addr,
576 unsigned long len, struct audlpa_ion_region **region)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577{
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530578 struct audlpa_ion_region *region_elt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579 int match_count = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580 *region = NULL;
581
582 /* returns physical address or zero */
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530583 list_for_each_entry(region_elt, &audio->ion_region_queue, list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584 if (addr >= region_elt->vaddr &&
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530585 addr < region_elt->vaddr + region_elt->len &&
586 addr + len <= region_elt->vaddr + region_elt->len) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700587 /* offset since we could pass vaddr inside a registerd
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530588 * ion buffer
589 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590
591 match_count++;
592 if (!*region)
593 *region = region_elt;
594 }
595 }
596
597 if (match_count > 1) {
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530598 pr_err("%s[%p]:multiple hits for vaddr %p, len %ld\n",
599 __func__, audio, addr, len);
600 list_for_each_entry(region_elt, &audio->ion_region_queue,
601 list) {
602 if (addr >= region_elt->vaddr &&
603 addr < region_elt->vaddr + region_elt->len &&
604 addr + len <= region_elt->vaddr + region_elt->len)
605 pr_err("\t%s[%p]:%p, %ld --> %p\n",
606 __func__, audio,
607 region_elt->vaddr,
608 region_elt->len,
609 (void *)region_elt->paddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700610 }
611 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700612 return *region ? 0 : -1;
613}
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530614static unsigned long audlpa_ion_fixup(struct audio *audio, void *addr,
615 unsigned long len, int ref_up)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700616{
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530617 struct audlpa_ion_region *region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700618 unsigned long paddr;
619 int ret;
620
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530621 ret = audlpa_ion_lookup_vaddr(audio, addr, len, &region);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700622 if (ret) {
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530623 pr_err("%s[%p]:lookup (%p, %ld) failed\n",
624 __func__, audio, addr, len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625 return 0;
626 }
627 if (ref_up)
628 region->ref_cnt++;
629 else
630 region->ref_cnt--;
631 paddr = region->paddr + (addr - region->vaddr);
632 return paddr;
633}
634
635/* audio -> lock must be held at this point */
636static int audlpa_aio_buf_add(struct audio *audio, unsigned dir,
637 void __user *arg)
638{
639 struct audlpa_buffer_node *buf_node;
640
641 buf_node = kmalloc(sizeof(*buf_node), GFP_KERNEL);
642
643 if (!buf_node)
644 return -ENOMEM;
645
646 if (copy_from_user(&buf_node->buf, arg, sizeof(buf_node->buf))) {
647 kfree(buf_node);
648 return -EFAULT;
649 }
650
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530651 buf_node->paddr = audlpa_ion_fixup(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700652 audio, buf_node->buf.buf_addr,
653 buf_node->buf.buf_len, 1);
654 if (dir) {
655 /* write */
656 if (!buf_node->paddr ||
657 (buf_node->paddr & 0x1) ||
658 (buf_node->buf.data_len & 0x1)) {
659 kfree(buf_node);
660 return -EINVAL;
661 }
662 list_add_tail(&buf_node->list, &audio->out_queue);
663 pr_debug("%s, Added to list: addr: %lx, length = %d\n",
664 __func__, buf_node->paddr, buf_node->buf.data_len);
665 audlpa_async_send_data(audio, 0, 0);
666 } else {
667 /* read */
Vasudeva Rao Thumati86edf6c2011-07-06 16:25:13 +0530668 kfree(buf_node);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700669 }
670 return 0;
671}
672
673static int config(struct audio *audio)
674{
675 int rc = 0;
676 if (!audio->out_prefill) {
677 if (audio->codec_ops.set_params != NULL) {
678 rc = audio->codec_ops.set_params(audio);
679 audio->out_prefill = 1;
680 }
681 }
682 return rc;
683}
684
685void q6_audlpa_out_cb(uint32_t opcode, uint32_t token,
686 uint32_t *payload, void *priv)
687{
688 struct audio *audio = (struct audio *) priv;
689
690 switch (opcode) {
691 case ASM_DATA_EVENT_WRITE_DONE:
692 pr_debug("%s: ASM_DATA_EVENT_WRITE_DONE, token = 0x%x\n",
693 __func__, token);
694 audlpa_async_send_data(audio, 1, token);
695 break;
696 case ASM_DATA_EVENT_EOS:
697 case ASM_DATA_CMDRSP_EOS:
698 pr_debug("%s: ASM_DATA_CMDRSP_EOS, teos = %d\n", __func__,
699 audio->teos);
700 if (audio->teos == 0) {
701 audio->teos = 1;
702 wake_up(&audio->write_wait);
703 }
704 break;
705 case ASM_SESSION_CMDRSP_GET_SESSION_TIME:
706 break;
Laxminath Kasam692c6542012-02-21 11:17:47 +0530707 case RESET_EVENTS:
708 reset_device();
709 break;
Deepa Madiregama14e9e762012-08-29 12:27:41 +0530710 case APR_BASIC_RSP_RESULT:
711 switch (payload[0]) {
712 case ASM_STREAM_CMD_CLOSE:
713 audlpa_unmap_ion_region(audio);
714 break;
715 default:
716 break;
717 }
718 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700719 default:
720 break;
721 }
722}
723
724static long pcm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
725{
726 pr_debug("%s: cmd = %d\n", __func__, cmd);
727 return -EINVAL;
728}
729
730static int audlpa_set_pcm_params(void *data)
731{
732 struct audio *audio = (struct audio *)data;
733 int rc;
734
735 rc = q6asm_media_format_block_pcm(audio->ac, audio->out_sample_rate,
736 audio->out_channel_mode);
737 if (rc < 0)
738 pr_err("%s: Format block pcm failed\n", __func__);
739 return rc;
740}
741
742static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
743{
744 struct audio *audio = file->private_data;
745 int rc = -EINVAL;
746 uint64_t timestamp;
747 uint64_t temp;
748
749 pr_debug("%s: audio_ioctl() cmd = %d\n", __func__, cmd);
750
751 if (cmd == AUDIO_GET_STATS) {
752 struct msm_audio_stats stats;
753
754 pr_debug("%s: AUDIO_GET_STATS cmd\n", __func__);
755 memset(&stats, 0, sizeof(stats));
756 timestamp = q6asm_get_session_time(audio->ac);
757 if (timestamp < 0) {
758 pr_err("%s: Get Session Time return value =%lld\n",
759 __func__, timestamp);
760 return -EAGAIN;
761 }
762 temp = (timestamp * 2 * audio->out_channel_mode);
763 temp = temp * (audio->out_sample_rate/1000);
764 temp = div_u64(temp, 1000);
765 audio->bytes_consumed = (uint32_t)(temp & 0xFFFFFFFF);
766 stats.byte_count = audio->bytes_consumed;
767 stats.unused[0] = (uint32_t)((temp >> 32) & 0xFFFFFFFF);
768 pr_debug("%s: bytes_consumed:lsb = %d, msb = %d,"
769 "timestamp = %lld\n", __func__,
770 audio->bytes_consumed, stats.unused[0], timestamp);
771 if (copy_to_user((void *) arg, &stats, sizeof(stats)))
772 return -EFAULT;
773 return 0;
774 }
775
776 switch (cmd) {
777 case AUDIO_ENABLE_AUDPP:
778 break;
779
780 case AUDIO_SET_VOLUME:
781 break;
782
783 case AUDIO_SET_PAN:
784 break;
785
786 case AUDIO_SET_EQ:
787 break;
788 }
789
790 if (cmd == AUDIO_GET_EVENT) {
791 pr_debug("%s: AUDIO_GET_EVENT\n", __func__);
792 if (mutex_trylock(&audio->get_event_lock)) {
793 rc = audlpa_process_event_req(audio,
794 (void __user *) arg);
795 mutex_unlock(&audio->get_event_lock);
796 } else
797 rc = -EBUSY;
798 return rc;
799 }
800
801 if (cmd == AUDIO_ABORT_GET_EVENT) {
802 audio->event_abort = 1;
803 wake_up(&audio->event_wait);
804 return 0;
805 }
806
807 mutex_lock(&audio->lock);
808 switch (cmd) {
809 case AUDIO_START:
810 pr_info("%s: AUDIO_START: Session %d\n", __func__,
811 audio->ac->session);
812 if (!audio->opened) {
813 pr_err("%s: Driver not opened\n", __func__);
814 rc = -EFAULT;
815 goto fail;
816 }
817 rc = config(audio);
818 if (rc) {
819 pr_err("%s: Out Configuration failed\n", __func__);
820 rc = -EFAULT;
821 goto fail;
822 }
823
824 rc = audio_enable(audio);
825 if (rc) {
826 pr_err("%s: audio enable failed\n", __func__);
827 rc = -EFAULT;
828 goto fail;
829 } else {
Swaminathan Sathappanb0021cd2011-08-31 15:20:12 -0700830 struct asm_softpause_params softpause = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700831 .enable = SOFT_PAUSE_ENABLE,
832 .period = SOFT_PAUSE_PERIOD,
833 .step = SOFT_PAUSE_STEP,
834 .rampingcurve = SOFT_PAUSE_CURVE_LINEAR,
835 };
Swaminathan Sathappanb0021cd2011-08-31 15:20:12 -0700836 struct asm_softvolume_params softvol = {
837 .period = SOFT_VOLUME_PERIOD,
838 .step = SOFT_VOLUME_STEP,
839 .rampingcurve = SOFT_VOLUME_CURVE_LINEAR,
840 };
Deepa Madiregama623b5932012-05-22 14:44:33 +0530841 if (softpause.rampingcurve == SOFT_PAUSE_CURVE_LINEAR)
842 softpause.step = SOFT_PAUSE_STEP_LINEAR;
843 if (softvol.rampingcurve == SOFT_VOLUME_CURVE_LINEAR)
844 softvol.step = SOFT_VOLUME_STEP_LINEAR;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845 audio->out_enabled = 1;
846 audio->out_needed = 1;
847 rc = q6asm_set_volume(audio->ac, audio->volume);
848 if (rc < 0)
849 pr_err("%s: Send Volume command failed rc=%d\n",
850 __func__, rc);
Swaminathan Sathappanb0021cd2011-08-31 15:20:12 -0700851 rc = q6asm_set_softpause(audio->ac, &softpause);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700852 if (rc < 0)
853 pr_err("%s: Send SoftPause Param failed rc=%d\n",
854 __func__, rc);
Swaminathan Sathappanb0021cd2011-08-31 15:20:12 -0700855 rc = q6asm_set_softvolume(audio->ac, &softvol);
856 if (rc < 0)
857 pr_err("%s: Send SoftVolume Param failed rc=%d\n",
858 __func__, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700859 rc = q6asm_set_lrgain(audio->ac, 0x2000, 0x2000);
860 if (rc < 0)
861 pr_err("%s: Send channel gain failed rc=%d\n",
862 __func__, rc);
863 /* disable mute by default */
864 rc = q6asm_set_mute(audio->ac, 0);
865 if (rc < 0)
866 pr_err("%s: Send mute command failed rc=%d\n",
867 __func__, rc);
868 if (!list_empty(&audio->out_queue))
869 pr_err("%s: write_list is not empty!!!\n",
870 __func__);
871 if (audio->stopped == 1)
872 audio->stopped = 0;
873 audlpa_prevent_sleep(audio);
874 }
875 break;
876
877 case AUDIO_STOP:
878 pr_info("%s: AUDIO_STOP: session_id:%d\n", __func__,
879 audio->ac->session);
880 audio->stopped = 1;
881 audlpa_async_flush(audio);
882 audio->out_enabled = 0;
883 audio->out_needed = 0;
884 audio->drv_status &= ~ADRV_STATUS_PAUSE;
885 audlpa_allow_sleep(audio);
886 break;
887
888 case AUDIO_FLUSH:
889 pr_debug("%s: AUDIO_FLUSH: session_id:%d\n", __func__,
890 audio->ac->session);
891 audio->wflush = 1;
892 if (audio->out_enabled)
893 audlpa_async_flush(audio);
894 else
895 audio->wflush = 0;
896 audio->wflush = 0;
897 break;
898
899 case AUDIO_SET_CONFIG:{
900 struct msm_audio_config config;
901 pr_debug("%s: AUDIO_SET_CONFIG\n", __func__);
902 if (copy_from_user(&config, (void *) arg, sizeof(config))) {
903 rc = -EFAULT;
904 pr_err("%s: ERROR: copy from user\n", __func__);
905 break;
906 }
907 if (!((config.channel_count == 1) ||
908 (config.channel_count == 2))) {
909 rc = -EINVAL;
910 pr_err("%s: ERROR: config.channel_count == %d\n",
911 __func__, config.channel_count);
912 break;
913 }
914
915 if (!((config.bits == 8) || (config.bits == 16) ||
916 (config.bits == 24))) {
917 rc = -EINVAL;
918 pr_err("%s: ERROR: config.bits = %d\n", __func__,
919 config.bits);
920 break;
921 }
922 audio->out_sample_rate = config.sample_rate;
923 audio->out_channel_mode = config.channel_count;
924 audio->out_bits = config.bits;
925 audio->buffer_count = config.buffer_count;
926 audio->buffer_size = config.buffer_size;
927 rc = 0;
928 break;
929 }
930
931 case AUDIO_GET_CONFIG:{
932 struct msm_audio_config config;
933 config.buffer_count = audio->buffer_count;
934 config.buffer_size = audio->buffer_size;
935 config.sample_rate = audio->out_sample_rate;
936 config.channel_count = audio->out_channel_mode;
937 config.bits = audio->out_bits;
938
939 config.meta_field = 0;
940 config.unused[0] = 0;
941 config.unused[1] = 0;
942 config.unused[2] = 0;
943 if (copy_to_user((void *) arg, &config, sizeof(config)))
944 rc = -EFAULT;
945 else
946 rc = 0;
947 break;
948 }
949
950 case AUDIO_PAUSE:
951 pr_debug("%s: AUDIO_PAUSE %ld\n", __func__, arg);
952 if (arg == 1) {
953 rc = audlpa_pause(audio);
954 if (rc < 0)
955 pr_err("%s: pause FAILED rc=%d\n", __func__,
956 rc);
957 audio->drv_status |= ADRV_STATUS_PAUSE;
958 } else if (arg == 0) {
959 if (audio->drv_status & ADRV_STATUS_PAUSE) {
960 rc = audio_enable(audio);
961 if (rc)
962 pr_err("%s: audio enable failed\n",
963 __func__);
964 else {
965 audio->drv_status &= ~ADRV_STATUS_PAUSE;
966 audio->out_enabled = 1;
967 }
968 }
969 }
970 break;
971
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530972 case AUDIO_REGISTER_ION: {
973 struct msm_audio_ion_info info;
974 pr_debug("%s: AUDIO_REGISTER_ION\n", __func__);
975 if (copy_from_user(&info, (void *)arg, sizeof(info)))
976 rc = -EFAULT;
977 else
978 rc = audlpa_ion_add(audio, &info);
979 break;
980 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700981
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530982 case AUDIO_DEREGISTER_ION: {
983 struct msm_audio_ion_info info;
984 pr_debug("%s: AUDIO_DEREGISTER_ION\n", __func__);
985 if (copy_from_user(&info, (void *)arg, sizeof(info)))
986 rc = -EFAULT;
987 else
988 rc = audlpa_ion_remove(audio, &info);
989 break;
990 }
991
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700992 case AUDIO_ASYNC_WRITE:
993 pr_debug("%s: AUDIO_ASYNC_WRITE\n", __func__);
994 if (audio->drv_status & ADRV_STATUS_FSYNC)
995 rc = -EBUSY;
996 else
997 rc = audlpa_aio_buf_add(audio, 1, (void __user *) arg);
998 break;
999
1000 case AUDIO_GET_SESSION_ID:
1001 if (copy_to_user((void *) arg, &audio->ac->session,
1002 sizeof(unsigned short)))
1003 return -EFAULT;
1004 rc = 0;
1005 break;
1006
1007 default:
1008 rc = audio->codec_ops.ioctl(file, cmd, arg);
1009 }
1010fail:
1011 mutex_unlock(&audio->lock);
1012 return rc;
1013}
1014
1015/* Only useful in tunnel-mode */
1016int audlpa_async_fsync(struct audio *audio)
1017{
1018 int rc = 0;
1019
1020 pr_info("%s:Session %d\n", __func__, audio->ac->session);
1021
1022 /* Blocking client sends more data */
1023 mutex_lock(&audio->lock);
1024 audio->drv_status |= ADRV_STATUS_FSYNC;
1025 mutex_unlock(&audio->lock);
1026
1027 mutex_lock(&audio->write_lock);
1028 audio->teos = 0;
1029
1030 rc = wait_event_interruptible(audio->write_wait,
1031 ((list_empty(&audio->out_queue)) ||
1032 audio->wflush || audio->stopped));
1033
1034 if (audio->wflush || audio->stopped)
1035 goto flush_event;
1036
1037 if (rc < 0) {
1038 pr_err("%s: wait event for list_empty failed, rc = %d\n",
1039 __func__, rc);
1040 goto done;
1041 }
1042
1043 rc = q6asm_cmd(audio->ac, CMD_EOS);
1044
1045 if (rc < 0) {
1046 pr_err("%s: q6asm_cmd failed, rc = %d", __func__, rc);
1047 goto done;
1048 }
1049 rc = wait_event_interruptible_timeout(audio->write_wait,
1050 (audio->teos || audio->wflush ||
1051 audio->stopped), 5*HZ);
1052
1053 if (rc < 0) {
1054 pr_err("%s: wait event for teos failed, rc = %d\n", __func__,
1055 rc);
1056 goto done;
1057 }
1058
1059 if (audio->teos == 1) {
1060 rc = audio_enable(audio);
1061 if (rc)
1062 pr_err("%s: audio enable failed\n", __func__);
1063 else {
1064 audio->drv_status &= ~ADRV_STATUS_PAUSE;
1065 audio->out_enabled = 1;
1066 audio->out_needed = 1;
1067 }
1068 }
1069
1070flush_event:
1071 if (audio->stopped || audio->wflush)
1072 rc = -EBUSY;
1073
1074done:
1075 mutex_unlock(&audio->write_lock);
1076 mutex_lock(&audio->lock);
1077 audio->drv_status &= ~ADRV_STATUS_FSYNC;
1078 mutex_unlock(&audio->lock);
1079
1080 return rc;
1081}
1082
Steve Mucklef132c6c2012-06-06 18:30:57 -07001083int audlpa_fsync(struct file *file, loff_t ppos1, loff_t ppos2, int datasync)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001084{
1085 struct audio *audio = file->private_data;
1086
1087 return audlpa_async_fsync(audio);
1088}
1089
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301090void audlpa_reset_ion_region(struct audio *audio)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001091{
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301092 struct audlpa_ion_region *region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001093 struct list_head *ptr, *next;
1094
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301095 list_for_each_safe(ptr, next, &audio->ion_region_queue) {
1096 region = list_entry(ptr, struct audlpa_ion_region, list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001097 list_del(&region->list);
Deepa Madiregamaa4795072012-06-15 22:35:52 +05301098 ion_unmap_kernel(audio->client, region->handle);
1099 ion_free(audio->client, region->handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001100 kfree(region);
1101 }
1102
1103 return;
1104}
1105
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301106static void audlpa_unmap_ion_region(struct audio *audio)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001107{
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301108 struct audlpa_ion_region *region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001109 struct list_head *ptr, *next;
1110 int rc = -EINVAL;
1111
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301112 pr_debug("%s[%p]:\n", __func__, audio);
1113 list_for_each_safe(ptr, next, &audio->ion_region_queue) {
1114 region = list_entry(ptr, struct audlpa_ion_region, list);
1115 pr_debug("%s[%p]: phy_address = 0x%lx\n",
1116 __func__, audio, region->paddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001117 if (region != NULL) {
1118 rc = q6asm_memory_unmap(audio->ac,
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301119 (uint32_t)region->paddr, IN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001120 if (rc < 0)
1121 pr_err("%s: memory unmap failed\n", __func__);
1122 }
1123 }
1124}
1125
1126static int audio_release(struct inode *inode, struct file *file)
1127{
1128 struct audio *audio = file->private_data;
1129
1130 pr_info("%s: audio instance 0x%08x freeing, session %d\n", __func__,
1131 (int)audio, audio->ac->session);
1132
1133 mutex_lock(&audio->lock);
1134 audio->wflush = 1;
1135 if (audio->out_enabled)
1136 audlpa_async_flush(audio);
1137 audio->wflush = 0;
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);
Deepa Madiregamaa4795072012-06-15 22:35:52 +05301143 ion_client_destroy(audio->client);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001144#ifdef CONFIG_HAS_EARLYSUSPEND
1145 unregister_early_suspend(&audio->suspend_ctl.node);
1146#endif
1147 audio->opened = 0;
1148 audio->out_enabled = 0;
1149 audio->out_prefill = 0;
1150 audio->event_abort = 1;
1151 wake_up(&audio->event_wait);
1152 audlpa_reset_event_queue(audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001153 if (audio->stopped == 0)
1154 audlpa_allow_sleep(audio);
1155 wake_lock_destroy(&audio->wakelock);
1156
1157 mutex_unlock(&audio->lock);
1158#ifdef CONFIG_DEBUG_FS
1159 if (audio->dentry)
1160 debugfs_remove(audio->dentry);
1161#endif
1162 kfree(audio);
1163 return 0;
1164}
1165
1166static void audlpa_post_event(struct audio *audio, int type,
1167 union msm_audio_event_payload payload)
1168{
1169 struct audlpa_event *e_node = NULL;
1170
1171 spin_lock(&audio->event_queue_lock);
1172
1173 pr_debug("%s:\n", __func__);
1174 if (!list_empty(&audio->free_event_queue)) {
1175 e_node = list_first_entry(&audio->free_event_queue,
1176 struct audlpa_event, list);
1177 list_del(&e_node->list);
1178 } else {
1179 e_node = kmalloc(sizeof(struct audlpa_event), GFP_ATOMIC);
1180 if (!e_node) {
1181 pr_err("%s: No mem to post event %d\n", __func__, type);
1182 return;
1183 }
1184 }
1185
1186 e_node->event_type = type;
1187 e_node->payload = payload;
1188
1189 list_add_tail(&e_node->list, &audio->event_queue);
1190 spin_unlock(&audio->event_queue_lock);
1191 wake_up(&audio->event_wait);
1192}
1193
1194#ifdef CONFIG_HAS_EARLYSUSPEND
1195static void audlpa_suspend(struct early_suspend *h)
1196{
1197 struct audlpa_suspend_ctl *ctl =
1198 container_of(h, struct audlpa_suspend_ctl, node);
1199 union msm_audio_event_payload payload;
1200
1201 pr_debug("%s:\n", __func__);
1202 audlpa_post_event(ctl->audio, AUDIO_EVENT_SUSPEND, payload);
1203}
1204
1205static void audlpa_resume(struct early_suspend *h)
1206{
1207 struct audlpa_suspend_ctl *ctl =
1208 container_of(h, struct audlpa_suspend_ctl, node);
1209 union msm_audio_event_payload payload;
1210
1211 pr_debug("%s:\n", __func__);
1212 audlpa_post_event(ctl->audio, AUDIO_EVENT_RESUME, payload);
1213}
1214#endif
1215
1216#ifdef CONFIG_DEBUG_FS
1217static ssize_t audlpa_debug_open(struct inode *inode, struct file *file)
1218{
1219 file->private_data = inode->i_private;
1220 return 0;
1221}
1222
1223static ssize_t audlpa_debug_read(struct file *file, char __user *buf,
1224 size_t count, loff_t *ppos)
1225{
1226 const int debug_bufmax = 4096;
1227 static char buffer[4096];
1228 int n = 0;
1229 struct audio *audio = file->private_data;
1230
1231 mutex_lock(&audio->lock);
1232 n = scnprintf(buffer, debug_bufmax, "opened %d\n", audio->opened);
1233 n += scnprintf(buffer + n, debug_bufmax - n,
1234 "out_enabled %d\n", audio->out_enabled);
1235 n += scnprintf(buffer + n, debug_bufmax - n,
1236 "stopped %d\n", audio->stopped);
1237 n += scnprintf(buffer + n, debug_bufmax - n,
1238 "volume %x\n", audio->volume);
1239 n += scnprintf(buffer + n, debug_bufmax - n,
1240 "sample rate %d\n",
1241 audio->out_sample_rate);
1242 n += scnprintf(buffer + n, debug_bufmax - n,
1243 "channel mode %d\n",
1244 audio->out_channel_mode);
1245 mutex_unlock(&audio->lock);
1246 /* Following variables are only useful for debugging when
1247 * when playback halts unexpectedly. Thus, no mutual exclusion
1248 * enforced
1249 */
1250 n += scnprintf(buffer + n, debug_bufmax - n,
1251 "wflush %d\n", audio->wflush);
1252 n += scnprintf(buffer + n, debug_bufmax - n,
1253 "running %d\n", audio->running);
1254 n += scnprintf(buffer + n, debug_bufmax - n,
1255 "out_needed %d\n", audio->out_needed);
1256 buffer[n] = 0;
1257 return simple_read_from_buffer(buf, count, ppos, buffer, n);
1258}
1259
1260static const struct file_operations audlpa_debug_fops = {
1261 .read = audlpa_debug_read,
1262 .open = audlpa_debug_open,
1263};
1264#endif
1265
1266static int audio_open(struct inode *inode, struct file *file)
1267{
1268 struct audio *audio = NULL;
1269 int rc, i, dec_attrb = 0;
1270 struct audlpa_event *e_node = NULL;
1271#ifdef CONFIG_DEBUG_FS
1272 /* 4 bytes represents decoder number, 1 byte for terminate string */
1273 char name[sizeof "msm_lpa_" + 5];
1274#endif
1275 char wake_lock_name[24];
1276
1277 /* Allocate audio instance, set to zero */
1278 audio = kzalloc(sizeof(struct audio), GFP_KERNEL);
1279 if (!audio) {
1280 pr_err("%s: no memory to allocate audio instance\n", __func__);
1281 rc = -ENOMEM;
1282 goto done;
1283 }
1284
1285 if ((file->f_mode & FMODE_WRITE) && !(file->f_mode & FMODE_READ)) {
1286 pr_debug("%s: Tunnel Mode playback\n", __func__);
1287 } else {
1288 kfree(audio);
1289 rc = -EACCES;
1290 goto done;
1291 }
1292
1293 /* Allocate the decoder based on inode minor number*/
1294 audio->minor_no = iminor(inode);
1295 dec_attrb |= audlpa_decs[audio->minor_no].dec_attrb;
1296 audio->codec_ops.ioctl = audlpa_decs[audio->minor_no].ioctl;
1297 audio->codec_ops.set_params = audlpa_decs[audio->minor_no].set_params;
1298 audio->buffer_size = BUFSZ;
1299 audio->buffer_count = MAX_BUF;
1300
1301 audio->ac = q6asm_audio_client_alloc((app_cb)q6_audlpa_out_cb,
1302 (void *)audio);
1303 if (!audio->ac) {
1304 pr_err("%s: Could not allocate memory for lpa client\n",
1305 __func__);
1306 rc = -ENOMEM;
1307 goto err;
1308 }
1309 rc = q6asm_open_write(audio->ac, FORMAT_LINEAR_PCM);
1310 if (rc < 0) {
1311 pr_err("%s: lpa out open failed\n", __func__);
1312 goto err;
1313 }
1314
1315 pr_debug("%s: Set mode to AIO session[%d]\n",
1316 __func__,
1317 audio->ac->session);
1318 rc = q6asm_set_io_mode(audio->ac, ASYNC_IO_MODE);
1319 if (rc < 0)
1320 pr_err("%s: Set IO mode failed\n", __func__);
1321
1322
1323 /* Initialize all locks of audio instance */
1324 mutex_init(&audio->lock);
1325 mutex_init(&audio->write_lock);
1326 mutex_init(&audio->get_event_lock);
1327 spin_lock_init(&audio->dsp_lock);
1328 init_waitqueue_head(&audio->write_wait);
1329 INIT_LIST_HEAD(&audio->out_queue);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301330 INIT_LIST_HEAD(&audio->ion_region_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001331 INIT_LIST_HEAD(&audio->free_event_queue);
1332 INIT_LIST_HEAD(&audio->event_queue);
1333 init_waitqueue_head(&audio->wait);
1334 init_waitqueue_head(&audio->event_wait);
1335 spin_lock_init(&audio->event_queue_lock);
1336 snprintf(wake_lock_name, sizeof wake_lock_name, "audio_lpa_%x",
1337 audio->ac->session);
1338 wake_lock_init(&audio->wakelock, WAKE_LOCK_SUSPEND, wake_lock_name);
1339
1340 audio->out_sample_rate = 44100;
1341 audio->out_channel_mode = 2;
1342 audio->out_bits = 16;
1343 audio->volume = 0x2000;
1344
1345 file->private_data = audio;
1346 audio->opened = 1;
1347 audio->out_enabled = 0;
1348 audio->out_prefill = 0;
1349 audio->bytes_consumed = 0;
1350
1351 audio->device_events = AUDDEV_EVT_STREAM_VOL_CHG;
1352 audio->drv_status &= ~ADRV_STATUS_PAUSE;
1353
1354 rc = auddev_register_evt_listner(audio->device_events,
1355 AUDDEV_CLNT_DEC,
1356 audio->ac->session,
1357 lpa_listner,
1358 (void *)audio);
1359 if (rc) {
1360 pr_err("%s: failed to register listner\n", __func__);
1361 goto err;
1362 }
1363
1364#ifdef CONFIG_DEBUG_FS
1365 snprintf(name, sizeof name, "msm_lpa_%04x", audio->ac->session);
1366 audio->dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
1367 NULL, (void *) audio, &audlpa_debug_fops);
1368
1369 if (IS_ERR(audio->dentry))
1370 pr_err("%s: debugfs_create_file failed\n", __func__);
1371#endif
1372#ifdef CONFIG_HAS_EARLYSUSPEND
1373 audio->suspend_ctl.node.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
1374 audio->suspend_ctl.node.resume = audlpa_resume;
1375 audio->suspend_ctl.node.suspend = audlpa_suspend;
1376 audio->suspend_ctl.audio = audio;
1377 register_early_suspend(&audio->suspend_ctl.node);
1378#endif
1379 for (i = 0; i < AUDLPA_EVENT_NUM; i++) {
1380 e_node = kmalloc(sizeof(struct audlpa_event), GFP_KERNEL);
1381 if (e_node)
1382 list_add_tail(&e_node->list, &audio->free_event_queue);
1383 else {
1384 pr_err("%s: event pkt alloc failed\n", __func__);
1385 break;
1386 }
1387 }
1388 pr_info("%s: audio instance 0x%08x created session[%d]\n", __func__,
1389 (int)audio,
1390 audio->ac->session);
Deepa Madiregamaa4795072012-06-15 22:35:52 +05301391 audio->client = msm_ion_client_create(UINT_MAX, "Audio_LPA_Client");
1392 if (IS_ERR_OR_NULL(audio->client)) {
1393 pr_err("Unable to create ION client\n");
1394 goto err;
1395 }
1396 pr_debug("Allocating ION clinet in audio_open %p", audio->client);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001397done:
1398 return rc;
1399err:
1400 q6asm_audio_client_free(audio->ac);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001401 kfree(audio);
1402 return rc;
1403}
1404
1405static const struct file_operations audio_lpa_fops = {
1406 .owner = THIS_MODULE,
1407 .open = audio_open,
1408 .release = audio_release,
1409 .unlocked_ioctl = audio_ioctl,
1410 .fsync = audlpa_fsync,
1411};
1412
1413static dev_t audlpa_devno;
1414static struct class *audlpa_class;
1415struct audlpa_device {
1416 const char *name;
1417 struct device *device;
1418 struct cdev cdev;
1419};
1420
1421static struct audlpa_device *audlpa_devices;
1422
1423static void audlpa_create(struct audlpa_device *adev, const char *name,
1424 struct device *parent, dev_t devt)
1425{
1426 struct device *dev;
1427 int rc;
1428
1429 dev = device_create(audlpa_class, parent, devt, "%s", name);
1430 if (IS_ERR(dev))
1431 return;
1432
1433 cdev_init(&adev->cdev, &audio_lpa_fops);
1434 adev->cdev.owner = THIS_MODULE;
1435
1436 rc = cdev_add(&adev->cdev, devt, 1);
1437 if (rc < 0) {
1438 device_destroy(audlpa_class, devt);
1439 } else {
1440 adev->device = dev;
1441 adev->name = name;
1442 }
1443}
1444
1445static int __init audio_init(void)
1446{
1447 int rc;
1448 int n = ARRAY_SIZE(audlpa_decs);
1449
1450 audlpa_devices = kzalloc(sizeof(struct audlpa_device) * n, GFP_KERNEL);
1451 if (!audlpa_devices)
1452 return -ENOMEM;
1453
1454 audlpa_class = class_create(THIS_MODULE, "audlpa");
1455 if (IS_ERR(audlpa_class))
1456 goto fail_create_class;
1457
1458 rc = alloc_chrdev_region(&audlpa_devno, 0, n, "msm_audio_lpa");
1459 if (rc < 0)
1460 goto fail_alloc_region;
1461
1462 for (n = 0; n < ARRAY_SIZE(audlpa_decs); n++) {
1463 audlpa_create(audlpa_devices + n,
1464 audlpa_decs[n].name, NULL,
1465 MKDEV(MAJOR(audlpa_devno), n));
1466 }
1467
1468 return 0;
1469
1470fail_alloc_region:
1471 class_unregister(audlpa_class);
1472 return rc;
1473fail_create_class:
1474 kfree(audlpa_devices);
1475 return -ENOMEM;
1476}
1477
1478static void __exit audio_exit(void)
1479{
1480 class_unregister(audlpa_class);
1481 kfree(audlpa_devices);
1482}
1483
1484module_init(audio_init);
1485module_exit(audio_exit);
1486
1487MODULE_DESCRIPTION("MSM LPA driver");
1488MODULE_LICENSE("GPL v2");