Merge branch 'sh/driver-core'
[pandora-kernel.git] / drivers / staging / dream / qdsp5 / audio_out.c
1 /* arch/arm/mach-msm/qdsp5/audio_out.c
2  *
3  * pcm audio output device
4  *
5  * Copyright (C) 2008 Google, Inc.
6  * Copyright (C) 2008 HTC Corporation
7  *
8  * This software is licensed under the terms of the GNU General Public
9  * License version 2, as published by the Free Software Foundation, and
10  * may be copied, distributed, and modified under those terms.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  */
18
19 #include <linux/module.h>
20 #include <linux/fs.h>
21 #include <linux/miscdevice.h>
22 #include <linux/uaccess.h>
23 #include <linux/kthread.h>
24 #include <linux/wait.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/debugfs.h>
27 #include <linux/delay.h>
28 #include <linux/wakelock.h>
29 #include <linux/gfp.h>
30
31 #include <linux/msm_audio.h>
32
33 #include <asm/atomic.h>
34 #include <asm/ioctls.h>
35 #include <mach/msm_adsp.h>
36
37 #include "audmgr.h"
38
39 #include <mach/qdsp5/qdsp5audppcmdi.h>
40 #include <mach/qdsp5/qdsp5audppmsg.h>
41
42 #include "evlog.h"
43
44 #define LOG_AUDIO_EVENTS 1
45 #define LOG_AUDIO_FAULTS 0
46
47 enum {
48         EV_NULL,
49         EV_OPEN,
50         EV_WRITE,
51         EV_RETURN,
52         EV_IOCTL,
53         EV_WRITE_WAIT,
54         EV_WAIT_EVENT,
55         EV_FILL_BUFFER,
56         EV_SEND_BUFFER,
57         EV_DSP_EVENT,
58         EV_ENABLE,
59 };
60
61 #if (LOG_AUDIO_EVENTS != 1)
62 static inline void LOG(unsigned id, unsigned arg) {}
63 #else
64 static const char *pcm_log_strings[] = {
65         "NULL",
66         "OPEN",
67         "WRITE",
68         "RETURN",
69         "IOCTL",
70         "WRITE_WAIT",
71         "WAIT_EVENT",
72         "FILL_BUFFER",
73         "SEND_BUFFER",
74         "DSP_EVENT",
75         "ENABLE",
76 };
77
78 DECLARE_LOG(pcm_log, 64, pcm_log_strings);
79
80 static int __init _pcm_log_init(void)
81 {
82         return ev_log_init(&pcm_log);
83 }
84 module_init(_pcm_log_init);
85
86 #define LOG(id,arg) ev_log_write(&pcm_log, id, arg)
87 #endif
88
89
90
91
92
93 #define BUFSZ (960 * 5)
94 #define DMASZ (BUFSZ * 2)
95
96 #define AUDPP_CMD_CFG_OBJ_UPDATE 0x8000
97 #define AUDPP_CMD_EQ_FLAG_DIS   0x0000
98 #define AUDPP_CMD_EQ_FLAG_ENA   -1
99 #define AUDPP_CMD_IIR_FLAG_DIS    0x0000
100 #define AUDPP_CMD_IIR_FLAG_ENA    -1
101
102 #define AUDPP_CMD_IIR_TUNING_FILTER  1
103 #define AUDPP_CMD_EQUALIZER     2
104 #define AUDPP_CMD_ADRC  3
105
106 #define ADRC_ENABLE  0x0001
107 #define EQ_ENABLE    0x0002
108 #define IIR_ENABLE   0x0004
109
110 struct adrc_filter {
111         uint16_t compression_th;
112         uint16_t compression_slope;
113         uint16_t rms_time;
114         uint16_t attack_const_lsw;
115         uint16_t attack_const_msw;
116         uint16_t release_const_lsw;
117         uint16_t release_const_msw;
118         uint16_t adrc_system_delay;
119 };
120
121 struct eqalizer {
122         uint16_t num_bands;
123         uint16_t eq_params[132];
124 };
125
126 struct rx_iir_filter {
127         uint16_t num_bands;
128         uint16_t iir_params[48];
129 };
130
131 typedef struct {
132         audpp_cmd_cfg_object_params_common common;
133         uint16_t eq_flag;
134         uint16_t num_bands;
135         uint16_t eq_params[132];
136 } audpp_cmd_cfg_object_params_eq;
137
138 typedef struct {
139         audpp_cmd_cfg_object_params_common common;
140         uint16_t active_flag;
141         uint16_t num_bands;
142         uint16_t iir_params[48];
143 } audpp_cmd_cfg_object_params_rx_iir;
144
145 struct buffer {
146         void *data;
147         unsigned size;
148         unsigned used;
149         unsigned addr;
150 };
151
152 struct audio {
153         struct buffer out[2];
154
155         spinlock_t dsp_lock;
156
157         uint8_t out_head;
158         uint8_t out_tail;
159         uint8_t out_needed; /* number of buffers the dsp is waiting for */
160
161         atomic_t out_bytes;
162
163         struct mutex lock;
164         struct mutex write_lock;
165         wait_queue_head_t wait;
166
167         /* configuration to use on next enable */
168         uint32_t out_sample_rate;
169         uint32_t out_channel_mode;
170         uint32_t out_weight;
171         uint32_t out_buffer_size;
172
173         struct audmgr audmgr;
174
175         /* data allocated for various buffers */
176         char *data;
177         dma_addr_t phys;
178
179         int opened;
180         int enabled;
181         int running;
182         int stopped; /* set when stopped, cleared on flush */
183         unsigned volume;
184
185         struct wake_lock wakelock;
186         struct wake_lock idlelock;
187
188         int adrc_enable;
189         struct adrc_filter adrc;
190
191         int eq_enable;
192         struct eqalizer eq;
193
194         int rx_iir_enable;
195         struct rx_iir_filter iir;
196 };
197
198 static void audio_prevent_sleep(struct audio *audio)
199 {
200         printk(KERN_INFO "++++++++++++++++++++++++++++++\n");
201         wake_lock(&audio->wakelock);
202         wake_lock(&audio->idlelock);
203 }
204
205 static void audio_allow_sleep(struct audio *audio)
206 {
207         wake_unlock(&audio->wakelock);
208         wake_unlock(&audio->idlelock);
209         printk(KERN_INFO "------------------------------\n");
210 }
211
212 static int audio_dsp_out_enable(struct audio *audio, int yes);
213 static int audio_dsp_send_buffer(struct audio *audio, unsigned id, unsigned len);
214 static int audio_dsp_set_adrc(struct audio *audio);
215 static int audio_dsp_set_eq(struct audio *audio);
216 static int audio_dsp_set_rx_iir(struct audio *audio);
217
218 static void audio_dsp_event(void *private, unsigned id, uint16_t *msg);
219
220 /* must be called with audio->lock held */
221 static int audio_enable(struct audio *audio)
222 {
223         struct audmgr_config cfg;
224         int rc;
225
226         pr_info("audio_enable()\n");
227
228         if (audio->enabled)
229                 return 0;
230
231         /* refuse to start if we're not ready */
232         if (!audio->out[0].used || !audio->out[1].used)
233                 return -EIO;
234
235         /* we start buffers 0 and 1, so buffer 0 will be the
236          * next one the dsp will want
237          */
238         audio->out_tail = 0;
239         audio->out_needed = 0;
240
241         cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
242         cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000;
243         cfg.def_method = RPC_AUD_DEF_METHOD_HOST_PCM;
244         cfg.codec = RPC_AUD_DEF_CODEC_PCM;
245         cfg.snd_method = RPC_SND_METHOD_MIDI;
246
247         audio_prevent_sleep(audio);
248         rc = audmgr_enable(&audio->audmgr, &cfg);
249         if (rc < 0) {
250                 audio_allow_sleep(audio);
251                 return rc;
252         }
253
254         if (audpp_enable(-1, audio_dsp_event, audio)) {
255                 pr_err("audio: audpp_enable() failed\n");
256                 audmgr_disable(&audio->audmgr);
257                 audio_allow_sleep(audio);
258                 return -ENODEV;
259         }
260
261         audio->enabled = 1;
262         return 0;
263 }
264
265 /* must be called with audio->lock held */
266 static int audio_disable(struct audio *audio)
267 {
268         pr_info("audio_disable()\n");
269         if (audio->enabled) {
270                 audio->enabled = 0;
271                 audio_dsp_out_enable(audio, 0);
272
273                 audpp_disable(-1, audio);
274
275                 wake_up(&audio->wait);
276                 audmgr_disable(&audio->audmgr);
277                 audio->out_needed = 0;
278                 audio_allow_sleep(audio);
279         }
280         return 0;
281 }
282
283 /* ------------------- dsp --------------------- */
284 static void audio_dsp_event(void *private, unsigned id, uint16_t *msg)
285 {
286         struct audio *audio = private;
287         struct buffer *frame;
288         unsigned long flags;
289
290         LOG(EV_DSP_EVENT, id);
291         switch (id) {
292         case AUDPP_MSG_HOST_PCM_INTF_MSG: {
293                 unsigned id = msg[2];
294                 unsigned idx = msg[3] - 1;
295
296                 /* pr_info("audio_dsp_event: HOST_PCM id %d idx %d\n", id, idx); */
297                 if (id != AUDPP_MSG_HOSTPCM_ID_ARM_RX) {
298                         pr_err("bogus id\n");
299                         break;
300                 }
301                 if (idx > 1) {
302                         pr_err("bogus buffer idx\n");
303                         break;
304                 }
305
306                 spin_lock_irqsave(&audio->dsp_lock, flags);
307                 if (audio->running) {
308                         atomic_add(audio->out[idx].used, &audio->out_bytes);
309                         audio->out[idx].used = 0;
310
311                         frame = audio->out + audio->out_tail;
312                         if (frame->used) {
313                                 audio_dsp_send_buffer(
314                                         audio, audio->out_tail, frame->used);
315                                 audio->out_tail ^= 1;
316                         } else {
317                                 audio->out_needed++;
318                         }
319                         wake_up(&audio->wait);
320                 }
321                 spin_unlock_irqrestore(&audio->dsp_lock, flags);
322                 break;
323         }
324         case AUDPP_MSG_PCMDMAMISSED:
325                 pr_info("audio_dsp_event: PCMDMAMISSED %d\n", msg[0]);
326                 break;
327         case AUDPP_MSG_CFG_MSG:
328                 if (msg[0] == AUDPP_MSG_ENA_ENA) {
329                         LOG(EV_ENABLE, 1);
330                         pr_info("audio_dsp_event: CFG_MSG ENABLE\n");
331                         audio->out_needed = 0;
332                         audio->running = 1;
333                         audpp_set_volume_and_pan(5, audio->volume, 0);
334                         audio_dsp_set_adrc(audio);
335                         audio_dsp_set_eq(audio);
336                         audio_dsp_set_rx_iir(audio);
337                         audio_dsp_out_enable(audio, 1);
338                 } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
339                         LOG(EV_ENABLE, 0);
340                         pr_info("audio_dsp_event: CFG_MSG DISABLE\n");
341                         audio->running = 0;
342                 } else {
343                         pr_err("audio_dsp_event: CFG_MSG %d?\n", msg[0]);
344                 }
345                 break;
346         default:
347                 pr_err("audio_dsp_event: UNKNOWN (%d)\n", id);
348         }
349 }
350
351 static int audio_dsp_out_enable(struct audio *audio, int yes)
352 {
353         audpp_cmd_pcm_intf cmd;
354
355         memset(&cmd, 0, sizeof(cmd));
356         cmd.cmd_id      = AUDPP_CMD_PCM_INTF_2;
357         cmd.object_num  = AUDPP_CMD_PCM_INTF_OBJECT_NUM;
358         cmd.config      = AUDPP_CMD_PCM_INTF_CONFIG_CMD_V;
359         cmd.intf_type   = AUDPP_CMD_PCM_INTF_RX_ENA_ARMTODSP_V;
360
361         if (yes) {
362                 cmd.write_buf1LSW       = audio->out[0].addr;
363                 cmd.write_buf1MSW       = audio->out[0].addr >> 16;
364                 cmd.write_buf1_len      = audio->out[0].size;
365                 cmd.write_buf2LSW       = audio->out[1].addr;
366                 cmd.write_buf2MSW       = audio->out[1].addr >> 16;
367                 cmd.write_buf2_len      = audio->out[1].size;
368                 cmd.arm_to_rx_flag      = AUDPP_CMD_PCM_INTF_ENA_V;
369                 cmd.weight_decoder_to_rx = audio->out_weight;
370                 cmd.weight_arm_to_rx    = 1;
371                 cmd.partition_number_arm_to_dsp = 0;
372                 cmd.sample_rate         = audio->out_sample_rate;
373                 cmd.channel_mode        = audio->out_channel_mode;
374         }
375
376         return audpp_send_queue2(&cmd, sizeof(cmd));
377 }
378
379 static int audio_dsp_send_buffer(struct audio *audio, unsigned idx, unsigned len)
380 {
381         audpp_cmd_pcm_intf_send_buffer cmd;
382
383         cmd.cmd_id              = AUDPP_CMD_PCM_INTF_2;
384         cmd.host_pcm_object     = AUDPP_CMD_PCM_INTF_OBJECT_NUM;
385         cmd.config              = AUDPP_CMD_PCM_INTF_BUFFER_CMD_V;
386         cmd.intf_type           = AUDPP_CMD_PCM_INTF_RX_ENA_ARMTODSP_V;
387         cmd.dsp_to_arm_buf_id   = 0;
388         cmd.arm_to_dsp_buf_id   = idx + 1;
389         cmd.arm_to_dsp_buf_len  = len;
390
391         LOG(EV_SEND_BUFFER, idx);
392         return audpp_send_queue2(&cmd, sizeof(cmd));
393 }
394
395 static int audio_dsp_set_adrc(struct audio *audio)
396 {
397         audpp_cmd_cfg_object_params_adrc cmd;
398
399         memset(&cmd, 0, sizeof(cmd));
400         cmd.common.comman_cfg = AUDPP_CMD_CFG_OBJ_UPDATE;
401         cmd.common.command_type = AUDPP_CMD_ADRC;
402
403         if (audio->adrc_enable) {
404                 cmd.adrc_flag = AUDPP_CMD_ADRC_FLAG_ENA;
405                 cmd.compression_th = audio->adrc.compression_th;
406                 cmd.compression_slope = audio->adrc.compression_slope;
407                 cmd.rms_time = audio->adrc.rms_time;
408                 cmd.attack_const_lsw = audio->adrc.attack_const_lsw;
409                 cmd.attack_const_msw = audio->adrc.attack_const_msw;
410                 cmd.release_const_lsw = audio->adrc.release_const_lsw;
411                 cmd.release_const_msw = audio->adrc.release_const_msw;
412                 cmd.adrc_system_delay = audio->adrc.adrc_system_delay;
413         } else {
414                 cmd.adrc_flag = AUDPP_CMD_ADRC_FLAG_DIS;
415         }
416         return audpp_send_queue3(&cmd, sizeof(cmd));
417 }
418
419 static int audio_dsp_set_eq(struct audio *audio)
420 {
421         audpp_cmd_cfg_object_params_eq cmd;
422
423         memset(&cmd, 0, sizeof(cmd));
424         cmd.common.comman_cfg = AUDPP_CMD_CFG_OBJ_UPDATE;
425         cmd.common.command_type = AUDPP_CMD_EQUALIZER;
426
427         if (audio->eq_enable) {
428                 cmd.eq_flag = AUDPP_CMD_EQ_FLAG_ENA;
429                 cmd.num_bands = audio->eq.num_bands;
430                 memcpy(&cmd.eq_params, audio->eq.eq_params,
431                        sizeof(audio->eq.eq_params));
432         } else {
433                 cmd.eq_flag = AUDPP_CMD_EQ_FLAG_DIS;
434         }
435         return audpp_send_queue3(&cmd, sizeof(cmd));
436 }
437
438 static int audio_dsp_set_rx_iir(struct audio *audio)
439 {
440         audpp_cmd_cfg_object_params_rx_iir cmd;
441
442         memset(&cmd, 0, sizeof(cmd));
443         cmd.common.comman_cfg = AUDPP_CMD_CFG_OBJ_UPDATE;
444         cmd.common.command_type = AUDPP_CMD_IIR_TUNING_FILTER;
445
446         if (audio->rx_iir_enable) {
447                 cmd.active_flag = AUDPP_CMD_IIR_FLAG_ENA;
448                 cmd.num_bands = audio->iir.num_bands;
449                 memcpy(&cmd.iir_params, audio->iir.iir_params,
450                        sizeof(audio->iir.iir_params));
451         } else {
452                 cmd.active_flag = AUDPP_CMD_IIR_FLAG_DIS;
453         }
454
455         return audpp_send_queue3(&cmd, sizeof(cmd));
456 }
457
458 /* ------------------- device --------------------- */
459
460 static int audio_enable_adrc(struct audio *audio, int enable)
461 {
462         if (audio->adrc_enable != enable) {
463                 audio->adrc_enable = enable;
464                 if (audio->running)
465                         audio_dsp_set_adrc(audio);
466         }
467         return 0;
468 }
469
470 static int audio_enable_eq(struct audio *audio, int enable)
471 {
472         if (audio->eq_enable != enable) {
473                 audio->eq_enable = enable;
474                 if (audio->running)
475                         audio_dsp_set_eq(audio);
476         }
477         return 0;
478 }
479
480 static int audio_enable_rx_iir(struct audio *audio, int enable)
481 {
482         if (audio->rx_iir_enable != enable) {
483                 audio->rx_iir_enable = enable;
484                 if (audio->running)
485                         audio_dsp_set_rx_iir(audio);
486         }
487         return 0;
488 }
489
490 static void audio_flush(struct audio *audio)
491 {
492         audio->out[0].used = 0;
493         audio->out[1].used = 0;
494         audio->out_head = 0;
495         audio->out_tail = 0;
496         audio->stopped = 0;
497 }
498
499 static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
500 {
501         struct audio *audio = file->private_data;
502         int rc;
503
504         if (cmd == AUDIO_GET_STATS) {
505                 struct msm_audio_stats stats;
506                 stats.byte_count = atomic_read(&audio->out_bytes);
507                 if (copy_to_user((void*) arg, &stats, sizeof(stats)))
508                         return -EFAULT;
509                 return 0;
510         }
511         if (cmd == AUDIO_SET_VOLUME) {
512                 unsigned long flags;
513                 spin_lock_irqsave(&audio->dsp_lock, flags);
514                 audio->volume = arg;
515                 if (audio->running)
516                         audpp_set_volume_and_pan(6, arg, 0);
517                 spin_unlock_irqrestore(&audio->dsp_lock, flags);
518         }
519
520         LOG(EV_IOCTL, cmd);
521         mutex_lock(&audio->lock);
522         switch (cmd) {
523         case AUDIO_START:
524                 rc = audio_enable(audio);
525                 break;
526         case AUDIO_STOP:
527                 rc = audio_disable(audio);
528                 audio->stopped = 1;
529                 break;
530         case AUDIO_FLUSH:
531                 if (audio->stopped) {
532                         /* Make sure we're stopped and we wake any threads
533                          * that might be blocked holding the write_lock.
534                          * While audio->stopped write threads will always
535                          * exit immediately.
536                          */
537                         wake_up(&audio->wait);
538                         mutex_lock(&audio->write_lock);
539                         audio_flush(audio);
540                         mutex_unlock(&audio->write_lock);
541                 }
542         case AUDIO_SET_CONFIG: {
543                 struct msm_audio_config config;
544                 if (copy_from_user(&config, (void*) arg, sizeof(config))) {
545                         rc = -EFAULT;
546                         break;
547                 }
548                 if (config.channel_count == 1) {
549                         config.channel_count = AUDPP_CMD_PCM_INTF_MONO_V;
550                 } else if (config.channel_count == 2) {
551                         config.channel_count= AUDPP_CMD_PCM_INTF_STEREO_V;
552                 } else {
553                         rc = -EINVAL;
554                         break;
555                 }
556                 audio->out_sample_rate = config.sample_rate;
557                 audio->out_channel_mode = config.channel_count;
558                 rc = 0;
559                 break;
560         }
561         case AUDIO_GET_CONFIG: {
562                 struct msm_audio_config config;
563                 config.buffer_size = BUFSZ;
564                 config.buffer_count = 2;
565                 config.sample_rate = audio->out_sample_rate;
566                 if (audio->out_channel_mode == AUDPP_CMD_PCM_INTF_MONO_V) {
567                         config.channel_count = 1;
568                 } else {
569                         config.channel_count = 2;
570                 }
571                 config.unused[0] = 0;
572                 config.unused[1] = 0;
573                 config.unused[2] = 0;
574                 config.unused[3] = 0;
575                 if (copy_to_user((void*) arg, &config, sizeof(config))) {
576                         rc = -EFAULT;
577                 } else {
578                         rc = 0;
579                 }
580                 break;
581         }
582         default:
583                 rc = -EINVAL;
584         }
585         mutex_unlock(&audio->lock);
586         return rc;
587 }
588
589 static ssize_t audio_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
590 {
591         return -EINVAL;
592 }
593
594 static inline int rt_policy(int policy)
595 {
596         if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR))
597                 return 1;
598         return 0;
599 }
600
601 static inline int task_has_rt_policy(struct task_struct *p)
602 {
603         return rt_policy(p->policy);
604 }
605
606 static ssize_t audio_write(struct file *file, const char __user *buf,
607                            size_t count, loff_t *pos)
608 {
609         struct sched_param s = { .sched_priority = 1 };
610         struct audio *audio = file->private_data;
611         unsigned long flags;
612         const char __user *start = buf;
613         struct buffer *frame;
614         size_t xfer;
615         int old_prio = current->rt_priority;
616         int old_policy = current->policy;
617         int cap_nice = cap_raised(current_cap(), CAP_SYS_NICE);
618         int rc = 0;
619
620         LOG(EV_WRITE, count | (audio->running << 28) | (audio->stopped << 24));
621
622         /* just for this write, set us real-time */
623         if (!task_has_rt_policy(current)) {
624                 struct cred *new = prepare_creds();
625                 cap_raise(new->cap_effective, CAP_SYS_NICE);
626                 commit_creds(new);
627                 sched_setscheduler(current, SCHED_RR, &s);
628         }
629
630         mutex_lock(&audio->write_lock);
631         while (count > 0) {
632                 frame = audio->out + audio->out_head;
633
634                 LOG(EV_WAIT_EVENT, 0);
635                 rc = wait_event_interruptible(audio->wait,
636                                               (frame->used == 0) || (audio->stopped));
637                 LOG(EV_WAIT_EVENT, 1);
638
639                 if (rc < 0)
640                         break;
641                 if (audio->stopped) {
642                         rc = -EBUSY;
643                         break;
644                 }
645                 xfer = count > frame->size ? frame->size : count;
646                 if (copy_from_user(frame->data, buf, xfer)) {
647                         rc = -EFAULT;
648                         break;
649                 }
650                 frame->used = xfer;
651                 audio->out_head ^= 1;
652                 count -= xfer;
653                 buf += xfer;
654
655                 spin_lock_irqsave(&audio->dsp_lock, flags);
656                 LOG(EV_FILL_BUFFER, audio->out_head ^ 1);
657                 frame = audio->out + audio->out_tail;
658                 if (frame->used && audio->out_needed) {
659                         audio_dsp_send_buffer(audio, audio->out_tail, frame->used);
660                         audio->out_tail ^= 1;
661                         audio->out_needed--;
662                 }
663                 spin_unlock_irqrestore(&audio->dsp_lock, flags);
664         }
665
666         mutex_unlock(&audio->write_lock);
667
668         /* restore scheduling policy and priority */
669         if (!rt_policy(old_policy)) {
670                 struct sched_param v = { .sched_priority = old_prio };
671                 sched_setscheduler(current, old_policy, &v);
672                 if (likely(!cap_nice)) {
673                         struct cred *new = prepare_creds();
674                         cap_lower(new->cap_effective, CAP_SYS_NICE);
675                         commit_creds(new);
676                         sched_setscheduler(current, SCHED_RR, &s);
677                 }
678         }
679
680         LOG(EV_RETURN,(buf > start) ? (buf - start) : rc);
681         if (buf > start)
682                 return buf - start;
683         return rc;
684 }
685
686 static int audio_release(struct inode *inode, struct file *file)
687 {
688         struct audio *audio = file->private_data;
689
690         LOG(EV_OPEN, 0);
691         mutex_lock(&audio->lock);
692         audio_disable(audio);
693         audio_flush(audio);
694         audio->opened = 0;
695         mutex_unlock(&audio->lock);
696         return 0;
697 }
698
699 static struct audio the_audio;
700
701 static int audio_open(struct inode *inode, struct file *file)
702 {
703         struct audio *audio = &the_audio;
704         int rc;
705
706         mutex_lock(&audio->lock);
707
708         if (audio->opened) {
709                 pr_err("audio: busy\n");
710                 rc = -EBUSY;
711                 goto done;
712         }
713
714         if (!audio->data) {
715                 audio->data = dma_alloc_coherent(NULL, DMASZ,
716                                                  &audio->phys, GFP_KERNEL);
717                 if (!audio->data) {
718                         pr_err("audio: could not allocate DMA buffers\n");
719                         rc = -ENOMEM;
720                         goto done;
721                 }
722         }
723
724         rc = audmgr_open(&audio->audmgr);
725         if (rc)
726                 goto done;
727
728         audio->out_buffer_size = BUFSZ;
729         audio->out_sample_rate = 44100;
730         audio->out_channel_mode = AUDPP_CMD_PCM_INTF_STEREO_V;
731         audio->out_weight = 100;
732
733         audio->out[0].data = audio->data + 0;
734         audio->out[0].addr = audio->phys + 0;
735         audio->out[0].size = BUFSZ;
736
737         audio->out[1].data = audio->data + BUFSZ;
738         audio->out[1].addr = audio->phys + BUFSZ;
739         audio->out[1].size = BUFSZ;
740
741         audio->volume = 0x2000;
742
743         audio_flush(audio);
744
745         file->private_data = audio;
746         audio->opened = 1;
747         rc = 0;
748         LOG(EV_OPEN, 1);
749 done:
750         mutex_unlock(&audio->lock);
751         return rc;
752 }
753
754 static long audpp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
755 {
756         struct audio *audio = file->private_data;
757         int rc = 0, enable;
758         uint16_t enable_mask;
759
760         mutex_lock(&audio->lock);
761         switch (cmd) {
762         case AUDIO_ENABLE_AUDPP:
763                 if (copy_from_user(&enable_mask, (void *) arg, sizeof(enable_mask)))
764                         goto out_fault;
765
766                 enable = (enable_mask & ADRC_ENABLE)? 1 : 0;
767                 audio_enable_adrc(audio, enable);
768                 enable = (enable_mask & EQ_ENABLE)? 1 : 0;
769                 audio_enable_eq(audio, enable);
770                 enable = (enable_mask & IIR_ENABLE)? 1 : 0;
771                 audio_enable_rx_iir(audio, enable);
772                 break;
773
774         case AUDIO_SET_ADRC:
775                 if (copy_from_user(&audio->adrc, (void*) arg, sizeof(audio->adrc)))
776                         goto out_fault;
777                 break;
778
779         case AUDIO_SET_EQ:
780                 if (copy_from_user(&audio->eq, (void*) arg, sizeof(audio->eq)))
781                         goto out_fault;
782                 break;
783
784         case AUDIO_SET_RX_IIR:
785                 if (copy_from_user(&audio->iir, (void*) arg, sizeof(audio->iir)))
786                         goto out_fault;
787                 break;
788
789         default:
790                 rc = -EINVAL;
791         }
792
793         goto out;
794
795  out_fault:
796         rc = -EFAULT;
797  out:
798         mutex_unlock(&audio->lock);
799         return rc;
800 }
801
802 static int audpp_open(struct inode *inode, struct file *file)
803 {
804         struct audio *audio = &the_audio;
805
806         file->private_data = audio;
807         return 0;
808 }
809
810 static struct file_operations audio_fops = {
811         .owner          = THIS_MODULE,
812         .open           = audio_open,
813         .release        = audio_release,
814         .read           = audio_read,
815         .write          = audio_write,
816         .unlocked_ioctl = audio_ioctl,
817 };
818
819 static struct file_operations audpp_fops = {
820         .owner          = THIS_MODULE,
821         .open           = audpp_open,
822         .unlocked_ioctl = audpp_ioctl,
823 };
824
825 struct miscdevice audio_misc = {
826         .minor  = MISC_DYNAMIC_MINOR,
827         .name   = "msm_pcm_out",
828         .fops   = &audio_fops,
829 };
830
831 struct miscdevice audpp_misc = {
832         .minor  = MISC_DYNAMIC_MINOR,
833         .name   = "msm_pcm_ctl",
834         .fops   = &audpp_fops,
835 };
836
837 static int __init audio_init(void)
838 {
839         mutex_init(&the_audio.lock);
840         mutex_init(&the_audio.write_lock);
841         spin_lock_init(&the_audio.dsp_lock);
842         init_waitqueue_head(&the_audio.wait);
843         wake_lock_init(&the_audio.wakelock, WAKE_LOCK_SUSPEND, "audio_pcm");
844         wake_lock_init(&the_audio.idlelock, WAKE_LOCK_IDLE, "audio_pcm_idle");
845         return (misc_register(&audio_misc) || misc_register(&audpp_misc));
846 }
847
848 device_initcall(audio_init);