blob: 733d5e334af1766795e94e3d043c1cd1de2564d0 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 * Copyright (C) 2009 Google, Inc.
Laxminath Kasam692c6542012-02-21 11:17:47 +05303 * Copyright (c) 2010-2012, Code Aurora Forum. 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__);
263 config.buffer_size = pcm->buffer_size;
264 config.buffer_count = pcm->buffer_count;
265 config.sample_rate = pcm->sample_rate;
266 config.channel_count = pcm->channel_count;
267 config.unused[0] = 0;
268 config.unused[1] = 0;
269 config.unused[2] = 0;
270 if (copy_to_user((void *) arg, &config, sizeof(config)))
271 rc = -EFAULT;
272 break;
273 }
274 case AUDIO_SET_EQ: {
275 struct msm_audio_eq_stream_config eq_config;
276 if (copy_from_user(&eq_config, (void *) arg,
277 sizeof(eq_config))) {
278 rc = -EFAULT;
279 break;
280 }
281 rc = q6asm_equalizer(pcm->ac, (void *) &eq_config);
282 if (rc < 0)
283 pr_err("%s: EQUALIZER FAILED\n", __func__);
284 break;
285 }
286 default:
287 rc = -EINVAL;
288 }
289 mutex_unlock(&pcm->lock);
290 return rc;
291}
292
293static int pcm_out_open(struct inode *inode, struct file *file)
294{
295 struct pcm *pcm;
296 int rc = 0;
297 char name[24];
298
299 pr_info("[%s:%s] open\n", __MM_FILE__, __func__);
300 pcm = kzalloc(sizeof(struct pcm), GFP_KERNEL);
301 if (!pcm) {
302 pr_err("%s: Failed to allocated memory\n", __func__);
303 return -ENOMEM;
304 }
305
306 pcm->channel_count = 2;
307 pcm->sample_rate = 44100;
308 pcm->buffer_size = BUFSZ;
309 pcm->buffer_count = MAX_BUF;
310 pcm->stream_event = AUDDEV_EVT_STREAM_VOL_CHG;
311 pcm->volume = 0x2000;
312
313 pcm->ac = q6asm_audio_client_alloc((app_cb)pcm_out_cb, (void *)pcm);
314 if (!pcm->ac) {
315 pr_err("%s: Could not allocate memory\n", __func__);
316 rc = -ENOMEM;
317 goto fail;
318 }
319
320 rc = q6asm_open_write(pcm->ac, FORMAT_LINEAR_PCM);
321 if (rc < 0) {
322 pr_err("%s: pcm out open failed for session %d\n", __func__,
323 pcm->ac->session);
324 rc = -EINVAL;
325 goto fail;
326 }
327
328 mutex_init(&pcm->lock);
329 mutex_init(&pcm->write_lock);
330 init_waitqueue_head(&pcm->write_wait);
331 spin_lock_init(&pcm->dsp_lock);
332 atomic_set(&pcm->out_enabled, 0);
333 atomic_set(&pcm->out_stopped, 0);
334 atomic_set(&pcm->out_count, pcm->buffer_count);
335 atomic_set(&pcm->out_prefill, 0);
336 atomic_set(&pcm->out_opened, 1);
337 snprintf(name, sizeof name, "audio_pcm_%x", pcm->ac->session);
338 wake_lock_init(&pcm->wakelock, WAKE_LOCK_SUSPEND, name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700339
340 rc = auddev_register_evt_listner(pcm->stream_event,
341 AUDDEV_CLNT_DEC,
342 pcm->ac->session,
343 pcm_event_listner,
344 (void *)pcm);
345 if (rc < 0) {
346 pr_err("%s: failed to register listner\n", __func__);
347 goto fail;
348 }
349
350 file->private_data = pcm;
351 pr_info("[%s:%s] open session id[%d]\n", __MM_FILE__,
352 __func__, pcm->ac->session);
353 return 0;
354fail:
355 if (pcm->ac)
356 q6asm_audio_client_free(pcm->ac);
357 kfree(pcm);
358 return rc;
359}
360
361static ssize_t pcm_out_write(struct file *file, const char __user *buf,
362 size_t count, loff_t *pos)
363{
364 struct pcm *pcm = file->private_data;
365 const char __user *start = buf;
366 int xfer;
367 char *bufptr;
368 uint32_t idx;
369 void *data;
370 int rc = 0;
371 uint32_t size;
372
373 if (!pcm->ac)
374 return -ENODEV;
375
376 if (!atomic_read(&pcm->out_enabled)) {
377 rc = config(pcm);
378 if (rc < 0)
379 return rc;
380 }
381
382 mutex_lock(&pcm->write_lock);
383 while (count > 0) {
384 rc = wait_event_timeout(pcm->write_wait,
385 (atomic_read(&pcm->out_count) ||
Amal Paulf6881072011-11-02 14:34:36 -0700386 atomic_read(&pcm->out_stopped)), 1 * HZ);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387 if (!rc) {
388 pr_err("%s: wait_event_timeout failed for session %d\n",
389 __func__, pcm->ac->session);
390 goto fail;
391 }
392
393 if (atomic_read(&pcm->out_stopped) &&
394 !atomic_read(&pcm->out_count)) {
395 pr_info("%s: pcm stopped out_count 0\n", __func__);
396 mutex_unlock(&pcm->write_lock);
397 return 0;
398 }
399
400 data = q6asm_is_cpu_buf_avail(IN, pcm->ac, &size, &idx);
401 bufptr = data;
402 if (bufptr) {
403 xfer = count;
404 if (xfer > BUFSZ)
405 xfer = BUFSZ;
406
407 if (copy_from_user(bufptr, buf, xfer)) {
408 rc = -EFAULT;
409 goto fail;
410 }
411 buf += xfer;
412 count -= xfer;
413 rc = q6asm_write(pcm->ac, xfer, 0, 0, NO_TIMESTAMP);
414 wmb();
415 if (rc < 0) {
416 rc = -EFAULT;
417 goto fail;
418 }
419 }
420 atomic_dec(&pcm->out_count);
421 }
422
423 rc = buf - start;
424fail:
425 mutex_unlock(&pcm->write_lock);
426 return rc;
427}
428
429static int pcm_out_release(struct inode *inode, struct file *file)
430{
431 struct pcm *pcm = file->private_data;
432
433 pr_info("[%s:%s] release session id[%d]\n", __MM_FILE__,
434 __func__, pcm->ac->session);
435 if (pcm->ac)
436 pcm_out_disable(pcm);
437 msm_clear_session_id(pcm->ac->session);
438 auddev_unregister_evt_listner(AUDDEV_CLNT_DEC, pcm->ac->session);
439 q6asm_audio_client_free(pcm->ac);
440 audio_allow_sleep(pcm);
441 wake_lock_destroy(&pcm->wakelock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700442 mutex_destroy(&pcm->lock);
443 mutex_destroy(&pcm->write_lock);
444 kfree(pcm);
445 return 0;
446}
447
448static const struct file_operations pcm_out_fops = {
449 .owner = THIS_MODULE,
450 .open = pcm_out_open,
451 .write = pcm_out_write,
452 .release = pcm_out_release,
453 .unlocked_ioctl = pcm_out_ioctl,
454};
455
456struct miscdevice pcm_out_misc = {
457 .minor = MISC_DYNAMIC_MINOR,
458 .name = "msm_pcm_out",
459 .fops = &pcm_out_fops,
460};
461
462static int __init pcm_out_init(void)
463{
464 return misc_register(&pcm_out_misc);
465}
466
467device_initcall(pcm_out_init);