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