blob: cbfee70c2476b2a91e96fdc5ff081e69092e65a1 [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;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700710 default:
711 break;
712 }
713}
714
715static long pcm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
716{
717 pr_debug("%s: cmd = %d\n", __func__, cmd);
718 return -EINVAL;
719}
720
721static int audlpa_set_pcm_params(void *data)
722{
723 struct audio *audio = (struct audio *)data;
724 int rc;
725
726 rc = q6asm_media_format_block_pcm(audio->ac, audio->out_sample_rate,
727 audio->out_channel_mode);
728 if (rc < 0)
729 pr_err("%s: Format block pcm failed\n", __func__);
730 return rc;
731}
732
733static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
734{
735 struct audio *audio = file->private_data;
736 int rc = -EINVAL;
737 uint64_t timestamp;
738 uint64_t temp;
739
740 pr_debug("%s: audio_ioctl() cmd = %d\n", __func__, cmd);
741
742 if (cmd == AUDIO_GET_STATS) {
743 struct msm_audio_stats stats;
744
745 pr_debug("%s: AUDIO_GET_STATS cmd\n", __func__);
746 memset(&stats, 0, sizeof(stats));
747 timestamp = q6asm_get_session_time(audio->ac);
748 if (timestamp < 0) {
749 pr_err("%s: Get Session Time return value =%lld\n",
750 __func__, timestamp);
751 return -EAGAIN;
752 }
753 temp = (timestamp * 2 * audio->out_channel_mode);
754 temp = temp * (audio->out_sample_rate/1000);
755 temp = div_u64(temp, 1000);
756 audio->bytes_consumed = (uint32_t)(temp & 0xFFFFFFFF);
757 stats.byte_count = audio->bytes_consumed;
758 stats.unused[0] = (uint32_t)((temp >> 32) & 0xFFFFFFFF);
759 pr_debug("%s: bytes_consumed:lsb = %d, msb = %d,"
760 "timestamp = %lld\n", __func__,
761 audio->bytes_consumed, stats.unused[0], timestamp);
762 if (copy_to_user((void *) arg, &stats, sizeof(stats)))
763 return -EFAULT;
764 return 0;
765 }
766
767 switch (cmd) {
768 case AUDIO_ENABLE_AUDPP:
769 break;
770
771 case AUDIO_SET_VOLUME:
772 break;
773
774 case AUDIO_SET_PAN:
775 break;
776
777 case AUDIO_SET_EQ:
778 break;
779 }
780
781 if (cmd == AUDIO_GET_EVENT) {
782 pr_debug("%s: AUDIO_GET_EVENT\n", __func__);
783 if (mutex_trylock(&audio->get_event_lock)) {
784 rc = audlpa_process_event_req(audio,
785 (void __user *) arg);
786 mutex_unlock(&audio->get_event_lock);
787 } else
788 rc = -EBUSY;
789 return rc;
790 }
791
792 if (cmd == AUDIO_ABORT_GET_EVENT) {
793 audio->event_abort = 1;
794 wake_up(&audio->event_wait);
795 return 0;
796 }
797
798 mutex_lock(&audio->lock);
799 switch (cmd) {
800 case AUDIO_START:
801 pr_info("%s: AUDIO_START: Session %d\n", __func__,
802 audio->ac->session);
803 if (!audio->opened) {
804 pr_err("%s: Driver not opened\n", __func__);
805 rc = -EFAULT;
806 goto fail;
807 }
808 rc = config(audio);
809 if (rc) {
810 pr_err("%s: Out Configuration failed\n", __func__);
811 rc = -EFAULT;
812 goto fail;
813 }
814
815 rc = audio_enable(audio);
816 if (rc) {
817 pr_err("%s: audio enable failed\n", __func__);
818 rc = -EFAULT;
819 goto fail;
820 } else {
Swaminathan Sathappanb0021cd2011-08-31 15:20:12 -0700821 struct asm_softpause_params softpause = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700822 .enable = SOFT_PAUSE_ENABLE,
823 .period = SOFT_PAUSE_PERIOD,
824 .step = SOFT_PAUSE_STEP,
825 .rampingcurve = SOFT_PAUSE_CURVE_LINEAR,
826 };
Swaminathan Sathappanb0021cd2011-08-31 15:20:12 -0700827 struct asm_softvolume_params softvol = {
828 .period = SOFT_VOLUME_PERIOD,
829 .step = SOFT_VOLUME_STEP,
830 .rampingcurve = SOFT_VOLUME_CURVE_LINEAR,
831 };
Deepa Madiregama623b5932012-05-22 14:44:33 +0530832 if (softpause.rampingcurve == SOFT_PAUSE_CURVE_LINEAR)
833 softpause.step = SOFT_PAUSE_STEP_LINEAR;
834 if (softvol.rampingcurve == SOFT_VOLUME_CURVE_LINEAR)
835 softvol.step = SOFT_VOLUME_STEP_LINEAR;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700836 audio->out_enabled = 1;
837 audio->out_needed = 1;
838 rc = q6asm_set_volume(audio->ac, audio->volume);
839 if (rc < 0)
840 pr_err("%s: Send Volume command failed rc=%d\n",
841 __func__, rc);
Swaminathan Sathappanb0021cd2011-08-31 15:20:12 -0700842 rc = q6asm_set_softpause(audio->ac, &softpause);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843 if (rc < 0)
844 pr_err("%s: Send SoftPause Param failed rc=%d\n",
845 __func__, rc);
Swaminathan Sathappanb0021cd2011-08-31 15:20:12 -0700846 rc = q6asm_set_softvolume(audio->ac, &softvol);
847 if (rc < 0)
848 pr_err("%s: Send SoftVolume Param failed rc=%d\n",
849 __func__, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700850 rc = q6asm_set_lrgain(audio->ac, 0x2000, 0x2000);
851 if (rc < 0)
852 pr_err("%s: Send channel gain failed rc=%d\n",
853 __func__, rc);
854 /* disable mute by default */
855 rc = q6asm_set_mute(audio->ac, 0);
856 if (rc < 0)
857 pr_err("%s: Send mute command failed rc=%d\n",
858 __func__, rc);
859 if (!list_empty(&audio->out_queue))
860 pr_err("%s: write_list is not empty!!!\n",
861 __func__);
862 if (audio->stopped == 1)
863 audio->stopped = 0;
864 audlpa_prevent_sleep(audio);
865 }
866 break;
867
868 case AUDIO_STOP:
869 pr_info("%s: AUDIO_STOP: session_id:%d\n", __func__,
870 audio->ac->session);
871 audio->stopped = 1;
872 audlpa_async_flush(audio);
873 audio->out_enabled = 0;
874 audio->out_needed = 0;
875 audio->drv_status &= ~ADRV_STATUS_PAUSE;
876 audlpa_allow_sleep(audio);
877 break;
878
879 case AUDIO_FLUSH:
880 pr_debug("%s: AUDIO_FLUSH: session_id:%d\n", __func__,
881 audio->ac->session);
882 audio->wflush = 1;
883 if (audio->out_enabled)
884 audlpa_async_flush(audio);
885 else
886 audio->wflush = 0;
887 audio->wflush = 0;
888 break;
889
890 case AUDIO_SET_CONFIG:{
891 struct msm_audio_config config;
892 pr_debug("%s: AUDIO_SET_CONFIG\n", __func__);
893 if (copy_from_user(&config, (void *) arg, sizeof(config))) {
894 rc = -EFAULT;
895 pr_err("%s: ERROR: copy from user\n", __func__);
896 break;
897 }
898 if (!((config.channel_count == 1) ||
899 (config.channel_count == 2))) {
900 rc = -EINVAL;
901 pr_err("%s: ERROR: config.channel_count == %d\n",
902 __func__, config.channel_count);
903 break;
904 }
905
906 if (!((config.bits == 8) || (config.bits == 16) ||
907 (config.bits == 24))) {
908 rc = -EINVAL;
909 pr_err("%s: ERROR: config.bits = %d\n", __func__,
910 config.bits);
911 break;
912 }
913 audio->out_sample_rate = config.sample_rate;
914 audio->out_channel_mode = config.channel_count;
915 audio->out_bits = config.bits;
916 audio->buffer_count = config.buffer_count;
917 audio->buffer_size = config.buffer_size;
918 rc = 0;
919 break;
920 }
921
922 case AUDIO_GET_CONFIG:{
923 struct msm_audio_config config;
924 config.buffer_count = audio->buffer_count;
925 config.buffer_size = audio->buffer_size;
926 config.sample_rate = audio->out_sample_rate;
927 config.channel_count = audio->out_channel_mode;
928 config.bits = audio->out_bits;
929
930 config.meta_field = 0;
931 config.unused[0] = 0;
932 config.unused[1] = 0;
933 config.unused[2] = 0;
934 if (copy_to_user((void *) arg, &config, sizeof(config)))
935 rc = -EFAULT;
936 else
937 rc = 0;
938 break;
939 }
940
941 case AUDIO_PAUSE:
942 pr_debug("%s: AUDIO_PAUSE %ld\n", __func__, arg);
943 if (arg == 1) {
944 rc = audlpa_pause(audio);
945 if (rc < 0)
946 pr_err("%s: pause FAILED rc=%d\n", __func__,
947 rc);
948 audio->drv_status |= ADRV_STATUS_PAUSE;
949 } else if (arg == 0) {
950 if (audio->drv_status & ADRV_STATUS_PAUSE) {
951 rc = audio_enable(audio);
952 if (rc)
953 pr_err("%s: audio enable failed\n",
954 __func__);
955 else {
956 audio->drv_status &= ~ADRV_STATUS_PAUSE;
957 audio->out_enabled = 1;
958 }
959 }
960 }
961 break;
962
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530963 case AUDIO_REGISTER_ION: {
964 struct msm_audio_ion_info info;
965 pr_debug("%s: AUDIO_REGISTER_ION\n", __func__);
966 if (copy_from_user(&info, (void *)arg, sizeof(info)))
967 rc = -EFAULT;
968 else
969 rc = audlpa_ion_add(audio, &info);
970 break;
971 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700972
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +0530973 case AUDIO_DEREGISTER_ION: {
974 struct msm_audio_ion_info info;
975 pr_debug("%s: AUDIO_DEREGISTER_ION\n", __func__);
976 if (copy_from_user(&info, (void *)arg, sizeof(info)))
977 rc = -EFAULT;
978 else
979 rc = audlpa_ion_remove(audio, &info);
980 break;
981 }
982
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700983 case AUDIO_ASYNC_WRITE:
984 pr_debug("%s: AUDIO_ASYNC_WRITE\n", __func__);
985 if (audio->drv_status & ADRV_STATUS_FSYNC)
986 rc = -EBUSY;
987 else
988 rc = audlpa_aio_buf_add(audio, 1, (void __user *) arg);
989 break;
990
991 case AUDIO_GET_SESSION_ID:
992 if (copy_to_user((void *) arg, &audio->ac->session,
993 sizeof(unsigned short)))
994 return -EFAULT;
995 rc = 0;
996 break;
997
998 default:
999 rc = audio->codec_ops.ioctl(file, cmd, arg);
1000 }
1001fail:
1002 mutex_unlock(&audio->lock);
1003 return rc;
1004}
1005
1006/* Only useful in tunnel-mode */
1007int audlpa_async_fsync(struct audio *audio)
1008{
1009 int rc = 0;
1010
1011 pr_info("%s:Session %d\n", __func__, audio->ac->session);
1012
1013 /* Blocking client sends more data */
1014 mutex_lock(&audio->lock);
1015 audio->drv_status |= ADRV_STATUS_FSYNC;
1016 mutex_unlock(&audio->lock);
1017
1018 mutex_lock(&audio->write_lock);
1019 audio->teos = 0;
1020
1021 rc = wait_event_interruptible(audio->write_wait,
1022 ((list_empty(&audio->out_queue)) ||
1023 audio->wflush || audio->stopped));
1024
1025 if (audio->wflush || audio->stopped)
1026 goto flush_event;
1027
1028 if (rc < 0) {
1029 pr_err("%s: wait event for list_empty failed, rc = %d\n",
1030 __func__, rc);
1031 goto done;
1032 }
1033
1034 rc = q6asm_cmd(audio->ac, CMD_EOS);
1035
1036 if (rc < 0) {
1037 pr_err("%s: q6asm_cmd failed, rc = %d", __func__, rc);
1038 goto done;
1039 }
1040 rc = wait_event_interruptible_timeout(audio->write_wait,
1041 (audio->teos || audio->wflush ||
1042 audio->stopped), 5*HZ);
1043
1044 if (rc < 0) {
1045 pr_err("%s: wait event for teos failed, rc = %d\n", __func__,
1046 rc);
1047 goto done;
1048 }
1049
1050 if (audio->teos == 1) {
1051 rc = audio_enable(audio);
1052 if (rc)
1053 pr_err("%s: audio enable failed\n", __func__);
1054 else {
1055 audio->drv_status &= ~ADRV_STATUS_PAUSE;
1056 audio->out_enabled = 1;
1057 audio->out_needed = 1;
1058 }
1059 }
1060
1061flush_event:
1062 if (audio->stopped || audio->wflush)
1063 rc = -EBUSY;
1064
1065done:
1066 mutex_unlock(&audio->write_lock);
1067 mutex_lock(&audio->lock);
1068 audio->drv_status &= ~ADRV_STATUS_FSYNC;
1069 mutex_unlock(&audio->lock);
1070
1071 return rc;
1072}
1073
Steve Mucklef132c6c2012-06-06 18:30:57 -07001074int audlpa_fsync(struct file *file, loff_t ppos1, loff_t ppos2, int datasync)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001075{
1076 struct audio *audio = file->private_data;
1077
1078 return audlpa_async_fsync(audio);
1079}
1080
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301081void audlpa_reset_ion_region(struct audio *audio)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001082{
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301083 struct audlpa_ion_region *region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001084 struct list_head *ptr, *next;
1085
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301086 list_for_each_safe(ptr, next, &audio->ion_region_queue) {
1087 region = list_entry(ptr, struct audlpa_ion_region, list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001088 list_del(&region->list);
Deepa Madiregamaa4795072012-06-15 22:35:52 +05301089 ion_unmap_kernel(audio->client, region->handle);
1090 ion_free(audio->client, region->handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001091 kfree(region);
1092 }
1093
1094 return;
1095}
1096
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301097static void audlpa_unmap_ion_region(struct audio *audio)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001098{
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301099 struct audlpa_ion_region *region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001100 struct list_head *ptr, *next;
1101 int rc = -EINVAL;
1102
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301103 pr_debug("%s[%p]:\n", __func__, audio);
1104 list_for_each_safe(ptr, next, &audio->ion_region_queue) {
1105 region = list_entry(ptr, struct audlpa_ion_region, list);
1106 pr_debug("%s[%p]: phy_address = 0x%lx\n",
1107 __func__, audio, region->paddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001108 if (region != NULL) {
1109 rc = q6asm_memory_unmap(audio->ac,
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301110 (uint32_t)region->paddr, IN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001111 if (rc < 0)
1112 pr_err("%s: memory unmap failed\n", __func__);
1113 }
1114 }
1115}
1116
1117static int audio_release(struct inode *inode, struct file *file)
1118{
1119 struct audio *audio = file->private_data;
1120
1121 pr_info("%s: audio instance 0x%08x freeing, session %d\n", __func__,
1122 (int)audio, audio->ac->session);
1123
1124 mutex_lock(&audio->lock);
1125 audio->wflush = 1;
1126 if (audio->out_enabled)
1127 audlpa_async_flush(audio);
1128 audio->wflush = 0;
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301129 audlpa_unmap_ion_region(audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001130 audio_disable(audio);
1131 msm_clear_session_id(audio->ac->session);
1132 auddev_unregister_evt_listner(AUDDEV_CLNT_DEC, audio->ac->session);
1133 q6asm_audio_client_free(audio->ac);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301134 audlpa_reset_ion_region(audio);
Deepa Madiregamaa4795072012-06-15 22:35:52 +05301135 ion_client_destroy(audio->client);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001136#ifdef CONFIG_HAS_EARLYSUSPEND
1137 unregister_early_suspend(&audio->suspend_ctl.node);
1138#endif
1139 audio->opened = 0;
1140 audio->out_enabled = 0;
1141 audio->out_prefill = 0;
1142 audio->event_abort = 1;
1143 wake_up(&audio->event_wait);
1144 audlpa_reset_event_queue(audio);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001145 if (audio->stopped == 0)
1146 audlpa_allow_sleep(audio);
1147 wake_lock_destroy(&audio->wakelock);
1148
1149 mutex_unlock(&audio->lock);
1150#ifdef CONFIG_DEBUG_FS
1151 if (audio->dentry)
1152 debugfs_remove(audio->dentry);
1153#endif
1154 kfree(audio);
1155 return 0;
1156}
1157
1158static void audlpa_post_event(struct audio *audio, int type,
1159 union msm_audio_event_payload payload)
1160{
1161 struct audlpa_event *e_node = NULL;
1162
1163 spin_lock(&audio->event_queue_lock);
1164
1165 pr_debug("%s:\n", __func__);
1166 if (!list_empty(&audio->free_event_queue)) {
1167 e_node = list_first_entry(&audio->free_event_queue,
1168 struct audlpa_event, list);
1169 list_del(&e_node->list);
1170 } else {
1171 e_node = kmalloc(sizeof(struct audlpa_event), GFP_ATOMIC);
1172 if (!e_node) {
1173 pr_err("%s: No mem to post event %d\n", __func__, type);
1174 return;
1175 }
1176 }
1177
1178 e_node->event_type = type;
1179 e_node->payload = payload;
1180
1181 list_add_tail(&e_node->list, &audio->event_queue);
1182 spin_unlock(&audio->event_queue_lock);
1183 wake_up(&audio->event_wait);
1184}
1185
1186#ifdef CONFIG_HAS_EARLYSUSPEND
1187static void audlpa_suspend(struct early_suspend *h)
1188{
1189 struct audlpa_suspend_ctl *ctl =
1190 container_of(h, struct audlpa_suspend_ctl, node);
1191 union msm_audio_event_payload payload;
1192
1193 pr_debug("%s:\n", __func__);
1194 audlpa_post_event(ctl->audio, AUDIO_EVENT_SUSPEND, payload);
1195}
1196
1197static void audlpa_resume(struct early_suspend *h)
1198{
1199 struct audlpa_suspend_ctl *ctl =
1200 container_of(h, struct audlpa_suspend_ctl, node);
1201 union msm_audio_event_payload payload;
1202
1203 pr_debug("%s:\n", __func__);
1204 audlpa_post_event(ctl->audio, AUDIO_EVENT_RESUME, payload);
1205}
1206#endif
1207
1208#ifdef CONFIG_DEBUG_FS
1209static ssize_t audlpa_debug_open(struct inode *inode, struct file *file)
1210{
1211 file->private_data = inode->i_private;
1212 return 0;
1213}
1214
1215static ssize_t audlpa_debug_read(struct file *file, char __user *buf,
1216 size_t count, loff_t *ppos)
1217{
1218 const int debug_bufmax = 4096;
1219 static char buffer[4096];
1220 int n = 0;
1221 struct audio *audio = file->private_data;
1222
1223 mutex_lock(&audio->lock);
1224 n = scnprintf(buffer, debug_bufmax, "opened %d\n", audio->opened);
1225 n += scnprintf(buffer + n, debug_bufmax - n,
1226 "out_enabled %d\n", audio->out_enabled);
1227 n += scnprintf(buffer + n, debug_bufmax - n,
1228 "stopped %d\n", audio->stopped);
1229 n += scnprintf(buffer + n, debug_bufmax - n,
1230 "volume %x\n", audio->volume);
1231 n += scnprintf(buffer + n, debug_bufmax - n,
1232 "sample rate %d\n",
1233 audio->out_sample_rate);
1234 n += scnprintf(buffer + n, debug_bufmax - n,
1235 "channel mode %d\n",
1236 audio->out_channel_mode);
1237 mutex_unlock(&audio->lock);
1238 /* Following variables are only useful for debugging when
1239 * when playback halts unexpectedly. Thus, no mutual exclusion
1240 * enforced
1241 */
1242 n += scnprintf(buffer + n, debug_bufmax - n,
1243 "wflush %d\n", audio->wflush);
1244 n += scnprintf(buffer + n, debug_bufmax - n,
1245 "running %d\n", audio->running);
1246 n += scnprintf(buffer + n, debug_bufmax - n,
1247 "out_needed %d\n", audio->out_needed);
1248 buffer[n] = 0;
1249 return simple_read_from_buffer(buf, count, ppos, buffer, n);
1250}
1251
1252static const struct file_operations audlpa_debug_fops = {
1253 .read = audlpa_debug_read,
1254 .open = audlpa_debug_open,
1255};
1256#endif
1257
1258static int audio_open(struct inode *inode, struct file *file)
1259{
1260 struct audio *audio = NULL;
1261 int rc, i, dec_attrb = 0;
1262 struct audlpa_event *e_node = NULL;
1263#ifdef CONFIG_DEBUG_FS
1264 /* 4 bytes represents decoder number, 1 byte for terminate string */
1265 char name[sizeof "msm_lpa_" + 5];
1266#endif
1267 char wake_lock_name[24];
1268
1269 /* Allocate audio instance, set to zero */
1270 audio = kzalloc(sizeof(struct audio), GFP_KERNEL);
1271 if (!audio) {
1272 pr_err("%s: no memory to allocate audio instance\n", __func__);
1273 rc = -ENOMEM;
1274 goto done;
1275 }
1276
1277 if ((file->f_mode & FMODE_WRITE) && !(file->f_mode & FMODE_READ)) {
1278 pr_debug("%s: Tunnel Mode playback\n", __func__);
1279 } else {
1280 kfree(audio);
1281 rc = -EACCES;
1282 goto done;
1283 }
1284
1285 /* Allocate the decoder based on inode minor number*/
1286 audio->minor_no = iminor(inode);
1287 dec_attrb |= audlpa_decs[audio->minor_no].dec_attrb;
1288 audio->codec_ops.ioctl = audlpa_decs[audio->minor_no].ioctl;
1289 audio->codec_ops.set_params = audlpa_decs[audio->minor_no].set_params;
1290 audio->buffer_size = BUFSZ;
1291 audio->buffer_count = MAX_BUF;
1292
1293 audio->ac = q6asm_audio_client_alloc((app_cb)q6_audlpa_out_cb,
1294 (void *)audio);
1295 if (!audio->ac) {
1296 pr_err("%s: Could not allocate memory for lpa client\n",
1297 __func__);
1298 rc = -ENOMEM;
1299 goto err;
1300 }
1301 rc = q6asm_open_write(audio->ac, FORMAT_LINEAR_PCM);
1302 if (rc < 0) {
1303 pr_err("%s: lpa out open failed\n", __func__);
1304 goto err;
1305 }
1306
1307 pr_debug("%s: Set mode to AIO session[%d]\n",
1308 __func__,
1309 audio->ac->session);
1310 rc = q6asm_set_io_mode(audio->ac, ASYNC_IO_MODE);
1311 if (rc < 0)
1312 pr_err("%s: Set IO mode failed\n", __func__);
1313
1314
1315 /* Initialize all locks of audio instance */
1316 mutex_init(&audio->lock);
1317 mutex_init(&audio->write_lock);
1318 mutex_init(&audio->get_event_lock);
1319 spin_lock_init(&audio->dsp_lock);
1320 init_waitqueue_head(&audio->write_wait);
1321 INIT_LIST_HEAD(&audio->out_queue);
Chaithanya Krishna Bacharajuce3a8ca2012-02-16 15:08:01 +05301322 INIT_LIST_HEAD(&audio->ion_region_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001323 INIT_LIST_HEAD(&audio->free_event_queue);
1324 INIT_LIST_HEAD(&audio->event_queue);
1325 init_waitqueue_head(&audio->wait);
1326 init_waitqueue_head(&audio->event_wait);
1327 spin_lock_init(&audio->event_queue_lock);
1328 snprintf(wake_lock_name, sizeof wake_lock_name, "audio_lpa_%x",
1329 audio->ac->session);
1330 wake_lock_init(&audio->wakelock, WAKE_LOCK_SUSPEND, wake_lock_name);
1331
1332 audio->out_sample_rate = 44100;
1333 audio->out_channel_mode = 2;
1334 audio->out_bits = 16;
1335 audio->volume = 0x2000;
1336
1337 file->private_data = audio;
1338 audio->opened = 1;
1339 audio->out_enabled = 0;
1340 audio->out_prefill = 0;
1341 audio->bytes_consumed = 0;
1342
1343 audio->device_events = AUDDEV_EVT_STREAM_VOL_CHG;
1344 audio->drv_status &= ~ADRV_STATUS_PAUSE;
1345
1346 rc = auddev_register_evt_listner(audio->device_events,
1347 AUDDEV_CLNT_DEC,
1348 audio->ac->session,
1349 lpa_listner,
1350 (void *)audio);
1351 if (rc) {
1352 pr_err("%s: failed to register listner\n", __func__);
1353 goto err;
1354 }
1355
1356#ifdef CONFIG_DEBUG_FS
1357 snprintf(name, sizeof name, "msm_lpa_%04x", audio->ac->session);
1358 audio->dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
1359 NULL, (void *) audio, &audlpa_debug_fops);
1360
1361 if (IS_ERR(audio->dentry))
1362 pr_err("%s: debugfs_create_file failed\n", __func__);
1363#endif
1364#ifdef CONFIG_HAS_EARLYSUSPEND
1365 audio->suspend_ctl.node.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
1366 audio->suspend_ctl.node.resume = audlpa_resume;
1367 audio->suspend_ctl.node.suspend = audlpa_suspend;
1368 audio->suspend_ctl.audio = audio;
1369 register_early_suspend(&audio->suspend_ctl.node);
1370#endif
1371 for (i = 0; i < AUDLPA_EVENT_NUM; i++) {
1372 e_node = kmalloc(sizeof(struct audlpa_event), GFP_KERNEL);
1373 if (e_node)
1374 list_add_tail(&e_node->list, &audio->free_event_queue);
1375 else {
1376 pr_err("%s: event pkt alloc failed\n", __func__);
1377 break;
1378 }
1379 }
1380 pr_info("%s: audio instance 0x%08x created session[%d]\n", __func__,
1381 (int)audio,
1382 audio->ac->session);
Deepa Madiregamaa4795072012-06-15 22:35:52 +05301383 audio->client = msm_ion_client_create(UINT_MAX, "Audio_LPA_Client");
1384 if (IS_ERR_OR_NULL(audio->client)) {
1385 pr_err("Unable to create ION client\n");
1386 goto err;
1387 }
1388 pr_debug("Allocating ION clinet in audio_open %p", audio->client);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001389done:
1390 return rc;
1391err:
1392 q6asm_audio_client_free(audio->ac);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001393 kfree(audio);
1394 return rc;
1395}
1396
1397static const struct file_operations audio_lpa_fops = {
1398 .owner = THIS_MODULE,
1399 .open = audio_open,
1400 .release = audio_release,
1401 .unlocked_ioctl = audio_ioctl,
1402 .fsync = audlpa_fsync,
1403};
1404
1405static dev_t audlpa_devno;
1406static struct class *audlpa_class;
1407struct audlpa_device {
1408 const char *name;
1409 struct device *device;
1410 struct cdev cdev;
1411};
1412
1413static struct audlpa_device *audlpa_devices;
1414
1415static void audlpa_create(struct audlpa_device *adev, const char *name,
1416 struct device *parent, dev_t devt)
1417{
1418 struct device *dev;
1419 int rc;
1420
1421 dev = device_create(audlpa_class, parent, devt, "%s", name);
1422 if (IS_ERR(dev))
1423 return;
1424
1425 cdev_init(&adev->cdev, &audio_lpa_fops);
1426 adev->cdev.owner = THIS_MODULE;
1427
1428 rc = cdev_add(&adev->cdev, devt, 1);
1429 if (rc < 0) {
1430 device_destroy(audlpa_class, devt);
1431 } else {
1432 adev->device = dev;
1433 adev->name = name;
1434 }
1435}
1436
1437static int __init audio_init(void)
1438{
1439 int rc;
1440 int n = ARRAY_SIZE(audlpa_decs);
1441
1442 audlpa_devices = kzalloc(sizeof(struct audlpa_device) * n, GFP_KERNEL);
1443 if (!audlpa_devices)
1444 return -ENOMEM;
1445
1446 audlpa_class = class_create(THIS_MODULE, "audlpa");
1447 if (IS_ERR(audlpa_class))
1448 goto fail_create_class;
1449
1450 rc = alloc_chrdev_region(&audlpa_devno, 0, n, "msm_audio_lpa");
1451 if (rc < 0)
1452 goto fail_alloc_region;
1453
1454 for (n = 0; n < ARRAY_SIZE(audlpa_decs); n++) {
1455 audlpa_create(audlpa_devices + n,
1456 audlpa_decs[n].name, NULL,
1457 MKDEV(MAJOR(audlpa_devno), n));
1458 }
1459
1460 return 0;
1461
1462fail_alloc_region:
1463 class_unregister(audlpa_class);
1464 return rc;
1465fail_create_class:
1466 kfree(audlpa_devices);
1467 return -ENOMEM;
1468}
1469
1470static void __exit audio_exit(void)
1471{
1472 class_unregister(audlpa_class);
1473 kfree(audlpa_devices);
1474}
1475
1476module_init(audio_init);
1477module_exit(audio_exit);
1478
1479MODULE_DESCRIPTION("MSM LPA driver");
1480MODULE_LICENSE("GPL v2");