blob: c6ae427aa6098a4ab26302c39a06018cb1ca577c [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 * Copyright (C) 2009 Google, Inc.
Asish Bhattacharya0929f9a2013-10-30 19:37:18 +05303 * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004 * Author: Brian Swetland <swetland@google.com>
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17#include <linux/fs.h>
18#include <linux/module.h>
19#include <linux/miscdevice.h>
20#include <linux/mutex.h>
21#include <linux/sched.h>
22#include <linux/uaccess.h>
23#include <linux/wait.h>
24#include <linux/msm_audio.h>
25#include <linux/slab.h>
26#include <linux/wakelock.h>
27#include <asm/atomic.h>
28#include <sound/q6asm.h>
29#include <sound/apr_audio.h>
30#include <mach/debug_mm.h>
31#include <mach/qdsp6v2/audio_dev_ctl.h>
32
33#define MAX_BUF 2
34#define BUFSZ (4800)
35
36struct pcm {
37 struct mutex lock;
38 struct mutex write_lock;
39 spinlock_t dsp_lock;
40 wait_queue_head_t write_wait;
41 struct audio_client *ac;
42 uint32_t sample_rate;
43 uint32_t channel_count;
44 uint32_t buffer_size;
45 uint32_t buffer_count;
46 uint32_t rec_mode;
47 uint32_t stream_event;
48 uint32_t volume;
49 atomic_t out_count;
50 atomic_t out_enabled;
51 atomic_t out_opened;
52 atomic_t out_stopped;
53 atomic_t out_prefill;
54 struct wake_lock wakelock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070055};
56
57void pcm_out_cb(uint32_t opcode, uint32_t token,
58 uint32_t *payload, void *priv)
59{
60 struct pcm *pcm = (struct pcm *) priv;
61 unsigned long flags;
62
63 spin_lock_irqsave(&pcm->dsp_lock, flags);
64 switch (opcode) {
65 case ASM_DATA_EVENT_WRITE_DONE:
66 atomic_inc(&pcm->out_count);
67 wake_up(&pcm->write_wait);
68 break;
Laxminath Kasam692c6542012-02-21 11:17:47 +053069 case RESET_EVENTS:
70 reset_device();
71 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070072 default:
73 break;
74 }
75 spin_unlock_irqrestore(&pcm->dsp_lock, flags);
76}
77
78static void audio_prevent_sleep(struct pcm *audio)
79{
80 pr_debug("%s:\n", __func__);
81 wake_lock(&audio->wakelock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070082}
83
84static void audio_allow_sleep(struct pcm *audio)
85{
86 pr_debug("%s:\n", __func__);
87 wake_unlock(&audio->wakelock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070088}
89
90static int pcm_out_enable(struct pcm *pcm)
91{
92 if (atomic_read(&pcm->out_enabled))
93 return 0;
94 return q6asm_run(pcm->ac, 0, 0, 0);
95}
96
97static int pcm_out_disable(struct pcm *pcm)
98{
99 int rc = 0;
100
101 if (atomic_read(&pcm->out_opened)) {
102 atomic_set(&pcm->out_enabled, 0);
103 atomic_set(&pcm->out_opened, 0);
104 rc = q6asm_cmd(pcm->ac, CMD_CLOSE);
105
106 atomic_set(&pcm->out_stopped, 1);
107 wake_up(&pcm->write_wait);
108 }
109 return rc;
110}
111
112static int config(struct pcm *pcm)
113{
114 int rc = 0;
115 if (!atomic_read(&pcm->out_prefill)) {
116 pr_debug("%s: pcm prefill\n", __func__);
117 rc = q6asm_audio_client_buf_alloc(IN, pcm->ac,
118 pcm->buffer_size, pcm->buffer_count);
119 if (rc < 0) {
120 pr_err("Audio Start: Buffer Allocation failed \
121 rc = %d\n", rc);
122 goto fail;
123 }
124
125 rc = q6asm_media_format_block_pcm(pcm->ac, pcm->sample_rate,
126 pcm->channel_count);
127 if (rc < 0)
128 pr_err("%s: CMD Format block failed\n", __func__);
129
130 atomic_set(&pcm->out_prefill, 1);
131 atomic_set(&pcm->out_count, pcm->buffer_count);
132 }
133fail:
134 return rc;
135}
136
137static void pcm_event_listner(u32 evt_id, union auddev_evt_data *evt_payload,
138 void *private_data)
139{
140 struct pcm *pcm = (struct pcm *) private_data;
141 int rc = 0;
142
143 switch (evt_id) {
144 case AUDDEV_EVT_STREAM_VOL_CHG:
145 pcm->volume = evt_payload->session_vol;
146 pr_debug("%s: AUDDEV_EVT_STREAM_VOL_CHG, stream vol %d, "
147 "enabled = %d\n", __func__, pcm->volume,
148 atomic_read(&pcm->out_enabled));
149 if (atomic_read(&pcm->out_enabled)) {
150 if (pcm->ac) {
151 rc = q6asm_set_volume(pcm->ac, pcm->volume);
152 if (rc < 0)
153 pr_err("%s: Send Volume command"
154 "failed rc=%d\n", __func__, rc);
155 }
156 }
157 break;
158 default:
159 pr_err("%s:ERROR:wrong event\n", __func__);
160 break;
161 }
162}
163
164static long pcm_out_ioctl(struct file *file, unsigned int cmd,
165 unsigned long arg)
166{
167 struct pcm *pcm = file->private_data;
168 int rc = 0;
169
170 if (cmd == AUDIO_GET_STATS) {
171 struct msm_audio_stats stats;
172 memset(&stats, 0, sizeof(stats));
173 if (copy_to_user((void *) arg, &stats, sizeof(stats)))
174 return -EFAULT;
175 return 0;
176 }
177
178 mutex_lock(&pcm->lock);
179 switch (cmd) {
180 case AUDIO_SET_VOLUME: {
181 int vol;
182 if (copy_from_user(&vol, (void *) arg, sizeof(vol))) {
183 rc = -EFAULT;
184 break;
185 }
186 break;
187 }
188 case AUDIO_START: {
189 pr_info("%s: AUDIO_START\n", __func__);
190 rc = config(pcm);
191 if (rc) {
192 pr_err("%s: Out Configuration failed\n", __func__);
193 rc = -EFAULT;
194 break;
195 }
196
197 rc = pcm_out_enable(pcm);
198 if (rc) {
199 pr_err("Out enable failed\n");
200 rc = -EFAULT;
201 break;
202 }
203 audio_prevent_sleep(pcm);
204 atomic_set(&pcm->out_enabled, 1);
205
206 rc = q6asm_set_volume(pcm->ac, pcm->volume);
207 if (rc < 0)
208 pr_err("%s: Send Volume command failed rc=%d\n",
209 __func__, rc);
210 rc = q6asm_set_lrgain(pcm->ac, 0x2000, 0x2000);
211 if (rc < 0)
212 pr_err("%s: Send channel gain failed rc=%d\n",
213 __func__, rc);
214 /* disable mute by default */
215 rc = q6asm_set_mute(pcm->ac, 0);
216 if (rc < 0)
217 pr_err("%s: Send mute command failed rc=%d\n",
218 __func__, rc);
219 break;
220 }
221 case AUDIO_GET_SESSION_ID: {
222 if (copy_to_user((void *) arg, &pcm->ac->session,
223 sizeof(unsigned short)))
224 rc = -EFAULT;
225 break;
226 }
227 case AUDIO_STOP:
228 break;
229 case AUDIO_FLUSH:
230 break;
231 case AUDIO_SET_CONFIG: {
232 struct msm_audio_config config;
233 pr_debug("%s: AUDIO_SET_CONFIG\n", __func__);
234 if (copy_from_user(&config, (void *) arg, sizeof(config))) {
235 rc = -EFAULT;
236 break;
237 }
238 if (config.channel_count < 1 || config.channel_count > 2) {
239 rc = -EINVAL;
240 break;
241 }
242 if (config.sample_rate < 8000 || config.sample_rate > 48000) {
243 rc = -EINVAL;
244 break;
245 }
246 if (config.buffer_size < 128) {
247 rc = -EINVAL;
248 break;
249 }
250 pcm->sample_rate = config.sample_rate;
251 pcm->channel_count = config.channel_count;
252 pcm->buffer_size = config.buffer_size;
253 pcm->buffer_count = config.buffer_count;
254 pr_debug("%s:buffer_size:%d buffer_count:%d sample_rate:%d \
255 channel_count:%d\n", __func__, pcm->buffer_size,
256 pcm->buffer_count, pcm->sample_rate,
257 pcm->channel_count);
258 break;
259 }
260 case AUDIO_GET_CONFIG: {
261 struct msm_audio_config config;
262 pr_debug("%s: AUDIO_GET_CONFIG\n", __func__);
Asish Bhattacharya0929f9a2013-10-30 19:37:18 +0530263 memset(&config, 0, sizeof(config));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700264 config.buffer_size = pcm->buffer_size;
265 config.buffer_count = pcm->buffer_count;
266 config.sample_rate = pcm->sample_rate;
267 config.channel_count = pcm->channel_count;
268 config.unused[0] = 0;
269 config.unused[1] = 0;
270 config.unused[2] = 0;
271 if (copy_to_user((void *) arg, &config, sizeof(config)))
272 rc = -EFAULT;
273 break;
274 }
275 case AUDIO_SET_EQ: {
276 struct msm_audio_eq_stream_config eq_config;
277 if (copy_from_user(&eq_config, (void *) arg,
278 sizeof(eq_config))) {
279 rc = -EFAULT;
280 break;
281 }
282 rc = q6asm_equalizer(pcm->ac, (void *) &eq_config);
283 if (rc < 0)
284 pr_err("%s: EQUALIZER FAILED\n", __func__);
285 break;
286 }
287 default:
288 rc = -EINVAL;
289 }
290 mutex_unlock(&pcm->lock);
291 return rc;
292}
293
294static int pcm_out_open(struct inode *inode, struct file *file)
295{
296 struct pcm *pcm;
297 int rc = 0;
298 char name[24];
299
300 pr_info("[%s:%s] open\n", __MM_FILE__, __func__);
301 pcm = kzalloc(sizeof(struct pcm), GFP_KERNEL);
302 if (!pcm) {
303 pr_err("%s: Failed to allocated memory\n", __func__);
304 return -ENOMEM;
305 }
306
307 pcm->channel_count = 2;
308 pcm->sample_rate = 44100;
309 pcm->buffer_size = BUFSZ;
310 pcm->buffer_count = MAX_BUF;
311 pcm->stream_event = AUDDEV_EVT_STREAM_VOL_CHG;
312 pcm->volume = 0x2000;
313
314 pcm->ac = q6asm_audio_client_alloc((app_cb)pcm_out_cb, (void *)pcm);
315 if (!pcm->ac) {
316 pr_err("%s: Could not allocate memory\n", __func__);
317 rc = -ENOMEM;
318 goto fail;
319 }
320
321 rc = q6asm_open_write(pcm->ac, FORMAT_LINEAR_PCM);
322 if (rc < 0) {
323 pr_err("%s: pcm out open failed for session %d\n", __func__,
324 pcm->ac->session);
325 rc = -EINVAL;
326 goto fail;
327 }
328
329 mutex_init(&pcm->lock);
330 mutex_init(&pcm->write_lock);
331 init_waitqueue_head(&pcm->write_wait);
332 spin_lock_init(&pcm->dsp_lock);
333 atomic_set(&pcm->out_enabled, 0);
334 atomic_set(&pcm->out_stopped, 0);
335 atomic_set(&pcm->out_count, pcm->buffer_count);
336 atomic_set(&pcm->out_prefill, 0);
337 atomic_set(&pcm->out_opened, 1);
338 snprintf(name, sizeof name, "audio_pcm_%x", pcm->ac->session);
339 wake_lock_init(&pcm->wakelock, WAKE_LOCK_SUSPEND, name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700340
341 rc = auddev_register_evt_listner(pcm->stream_event,
342 AUDDEV_CLNT_DEC,
343 pcm->ac->session,
344 pcm_event_listner,
345 (void *)pcm);
346 if (rc < 0) {
347 pr_err("%s: failed to register listner\n", __func__);
348 goto fail;
349 }
350
351 file->private_data = pcm;
352 pr_info("[%s:%s] open session id[%d]\n", __MM_FILE__,
353 __func__, pcm->ac->session);
354 return 0;
355fail:
356 if (pcm->ac)
357 q6asm_audio_client_free(pcm->ac);
358 kfree(pcm);
359 return rc;
360}
361
362static ssize_t pcm_out_write(struct file *file, const char __user *buf,
363 size_t count, loff_t *pos)
364{
365 struct pcm *pcm = file->private_data;
366 const char __user *start = buf;
367 int xfer;
368 char *bufptr;
369 uint32_t idx;
370 void *data;
371 int rc = 0;
372 uint32_t size;
373
374 if (!pcm->ac)
375 return -ENODEV;
376
377 if (!atomic_read(&pcm->out_enabled)) {
378 rc = config(pcm);
379 if (rc < 0)
380 return rc;
381 }
382
383 mutex_lock(&pcm->write_lock);
384 while (count > 0) {
385 rc = wait_event_timeout(pcm->write_wait,
386 (atomic_read(&pcm->out_count) ||
Amal Paulf6881072011-11-02 14:34:36 -0700387 atomic_read(&pcm->out_stopped)), 1 * HZ);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700388 if (!rc) {
389 pr_err("%s: wait_event_timeout failed for session %d\n",
390 __func__, pcm->ac->session);
391 goto fail;
392 }
393
394 if (atomic_read(&pcm->out_stopped) &&
395 !atomic_read(&pcm->out_count)) {
396 pr_info("%s: pcm stopped out_count 0\n", __func__);
397 mutex_unlock(&pcm->write_lock);
398 return 0;
399 }
400
401 data = q6asm_is_cpu_buf_avail(IN, pcm->ac, &size, &idx);
402 bufptr = data;
403 if (bufptr) {
404 xfer = count;
405 if (xfer > BUFSZ)
406 xfer = BUFSZ;
407
408 if (copy_from_user(bufptr, buf, xfer)) {
409 rc = -EFAULT;
410 goto fail;
411 }
412 buf += xfer;
413 count -= xfer;
414 rc = q6asm_write(pcm->ac, xfer, 0, 0, NO_TIMESTAMP);
415 wmb();
416 if (rc < 0) {
417 rc = -EFAULT;
418 goto fail;
419 }
420 }
421 atomic_dec(&pcm->out_count);
422 }
423
424 rc = buf - start;
425fail:
426 mutex_unlock(&pcm->write_lock);
427 return rc;
428}
429
430static int pcm_out_release(struct inode *inode, struct file *file)
431{
432 struct pcm *pcm = file->private_data;
433
434 pr_info("[%s:%s] release session id[%d]\n", __MM_FILE__,
435 __func__, pcm->ac->session);
436 if (pcm->ac)
437 pcm_out_disable(pcm);
438 msm_clear_session_id(pcm->ac->session);
439 auddev_unregister_evt_listner(AUDDEV_CLNT_DEC, pcm->ac->session);
440 q6asm_audio_client_free(pcm->ac);
441 audio_allow_sleep(pcm);
442 wake_lock_destroy(&pcm->wakelock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700443 mutex_destroy(&pcm->lock);
444 mutex_destroy(&pcm->write_lock);
445 kfree(pcm);
446 return 0;
447}
448
449static const struct file_operations pcm_out_fops = {
450 .owner = THIS_MODULE,
451 .open = pcm_out_open,
452 .write = pcm_out_write,
453 .release = pcm_out_release,
454 .unlocked_ioctl = pcm_out_ioctl,
455};
456
457struct miscdevice pcm_out_misc = {
458 .minor = MISC_DYNAMIC_MINOR,
459 .name = "msm_pcm_out",
460 .fops = &pcm_out_fops,
461};
462
463static int __init pcm_out_init(void)
464{
465 return misc_register(&pcm_out_misc);
466}
467
468device_initcall(pcm_out_init);