]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/staging/dream/qdsp5/audio_qcelp.c
Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jwessel...
[net-next-2.6.git] / drivers / staging / dream / qdsp5 / audio_qcelp.c
1 /* arch/arm/mach-msm/qdsp5/audio_qcelp.c
2  *
3  * qcelp 13k audio decoder device
4  *
5  * Copyright (c) 2008 QUALCOMM USA, INC.
6  *
7  * This code is based in part on audio_mp3.c, which is
8  * Copyright (C) 2008 Google, Inc.
9  * Copyright (C) 2008 HTC Corporation
10  *
11  * This software is licensed under the terms of the GNU General Public
12  * License version 2, as published by the Free Software Foundation, and
13  * may be copied, distributed, and modified under those terms.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  * See the GNU General Public License for more details.
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, you can find it at http://www.fsf.org.
22  *
23  */
24
25 #include <linux/module.h>
26 #include <linux/fs.h>
27 #include <linux/miscdevice.h>
28 #include <linux/uaccess.h>
29 #include <linux/sched.h>
30 #include <linux/wait.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/gfp.h>
33
34 #include <asm/ioctls.h>
35 #include <mach/msm_adsp.h>
36 #include <linux/msm_audio.h>
37 #include <mach/qdsp5/qdsp5audppcmdi.h>
38 #include <mach/qdsp5/qdsp5audppmsg.h>
39 #include <mach/qdsp5/qdsp5audplaycmdi.h>
40 #include <mach/qdsp5/qdsp5audplaymsg.h>
41
42 #include "audmgr.h"
43 /* for queue ids - should be relative to module number*/
44 #include "adsp.h"
45
46 #ifdef DEBUG
47 #define dprintk(format, arg...) \
48 printk(KERN_DEBUG format, ## arg)
49 #else
50 #define dprintk(format, arg...) do {} while (0)
51 #endif
52
53 #define BUFSZ 1080 /* QCELP 13K Hold 600ms packet data = 36 * 30 */
54 #define BUF_COUNT 2
55 #define DMASZ (BUFSZ * BUF_COUNT)
56
57 #define PCM_BUFSZ_MIN 1600 /* 100ms worth of data */
58 #define PCM_BUF_MAX_COUNT 5
59
60 #define AUDDEC_DEC_QCELP 9
61
62 #define ROUTING_MODE_FTRT       1
63 #define ROUTING_MODE_RT         2
64 /* Decoder status received from AUDPPTASK */
65 #define AUDPP_DEC_STATUS_SLEEP  0
66 #define AUDPP_DEC_STATUS_INIT   1
67 #define AUDPP_DEC_STATUS_CFG    2
68 #define AUDPP_DEC_STATUS_PLAY   3
69
70 struct buffer {
71         void *data;
72         unsigned size;
73         unsigned used;          /* Input usage actual DSP produced PCM size  */
74         unsigned addr;
75 };
76
77 struct audio {
78         struct buffer out[BUF_COUNT];
79
80         spinlock_t dsp_lock;
81
82         uint8_t out_head;
83         uint8_t out_tail;
84         uint8_t out_needed;     /* number of buffers the dsp is waiting for */
85
86         struct mutex lock;
87         struct mutex write_lock;
88         wait_queue_head_t write_wait;
89
90         /* Host PCM section - START */
91         struct buffer in[PCM_BUF_MAX_COUNT];
92         struct mutex read_lock;
93         wait_queue_head_t read_wait;    /* Wait queue for read */
94         char *read_data;        /* pointer to reader buffer */
95         dma_addr_t read_phys;   /* physical address of reader buffer */
96         uint8_t read_next;      /* index to input buffers to be read next */
97         uint8_t fill_next;      /* index to buffer that DSP should be filling */
98         uint8_t pcm_buf_count;  /* number of pcm buffer allocated */
99         /* Host PCM section - END */
100
101         struct msm_adsp_module *audplay;
102
103         struct audmgr audmgr;
104
105         /* data allocated for various buffers */
106         char *data;
107         dma_addr_t phys;
108
109         uint8_t opened:1;
110         uint8_t enabled:1;
111         uint8_t running:1;
112         uint8_t stopped:1;      /* set when stopped, cleared on flush */
113         uint8_t pcm_feedback:1; /* set when non-tunnel mode */
114         uint8_t buf_refresh:1;
115
116         unsigned volume;
117
118         uint16_t dec_id;
119 };
120
121 static struct audio the_qcelp_audio;
122
123 static int auddec_dsp_config(struct audio *audio, int enable);
124 static void audpp_cmd_cfg_adec_params(struct audio *audio);
125 static void audpp_cmd_cfg_routing_mode(struct audio *audio);
126 static void audqcelp_send_data(struct audio *audio, unsigned needed);
127 static void audqcelp_config_hostpcm(struct audio *audio);
128 static void audqcelp_buffer_refresh(struct audio *audio);
129 static void audqcelp_dsp_event(void *private, unsigned id, uint16_t *msg);
130
131 /* must be called with audio->lock held */
132 static int audqcelp_enable(struct audio *audio)
133 {
134         struct audmgr_config cfg;
135         int rc;
136
137         dprintk("audqcelp_enable()\n");
138
139         if (audio->enabled)
140                 return 0;
141
142         audio->out_tail = 0;
143         audio->out_needed = 0;
144
145         cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
146         cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000;
147         cfg.def_method = RPC_AUD_DEF_METHOD_PLAYBACK;
148         cfg.codec = RPC_AUD_DEF_CODEC_13K;
149         cfg.snd_method = RPC_SND_METHOD_MIDI;
150
151         rc = audmgr_enable(&audio->audmgr, &cfg);
152         if (rc < 0)
153                 return rc;
154
155         if (msm_adsp_enable(audio->audplay)) {
156                 pr_err("audio: msm_adsp_enable(audplay) failed\n");
157                 audmgr_disable(&audio->audmgr);
158                 return -ENODEV;
159         }
160
161         if (audpp_enable(audio->dec_id, audqcelp_dsp_event, audio)) {
162                 pr_err("audio: audpp_enable() failed\n");
163                 msm_adsp_disable(audio->audplay);
164                 audmgr_disable(&audio->audmgr);
165                 return -ENODEV;
166         }
167         audio->enabled = 1;
168         return 0;
169 }
170
171 /* must be called with audio->lock held */
172 static int audqcelp_disable(struct audio *audio)
173 {
174         dprintk("audqcelp_disable()\n");
175         if (audio->enabled) {
176                 audio->enabled = 0;
177                 auddec_dsp_config(audio, 0);
178                 wake_up(&audio->write_wait);
179                 wake_up(&audio->read_wait);
180                 msm_adsp_disable(audio->audplay);
181                 audpp_disable(audio->dec_id, audio);
182                 audmgr_disable(&audio->audmgr);
183                 audio->out_needed = 0;
184         }
185         return 0;
186 }
187
188 /* ------------------- dsp --------------------- */
189 static void audqcelp_update_pcm_buf_entry(struct audio *audio,
190         uint32_t *payload)
191 {
192         uint8_t index;
193         unsigned long flags;
194
195         spin_lock_irqsave(&audio->dsp_lock, flags);
196         for (index = 0; index < payload[1]; index++) {
197                 if (audio->in[audio->fill_next].addr ==
198                         payload[2 + index * 2]) {
199                         dprintk("audqcelp_update_pcm_buf_entry: in[%d] ready\n",
200                         audio->fill_next);
201                         audio->in[audio->fill_next].used =
202                         payload[3 + index * 2];
203                         if ((++audio->fill_next) == audio->pcm_buf_count)
204                                 audio->fill_next = 0;
205                 } else {
206                         pr_err(
207                         "audqcelp_update_pcm_buf_entry: expected=%x ret=%x\n",
208                         audio->in[audio->fill_next].addr,
209                         payload[1 + index * 2]);
210                         break;
211                 }
212         }
213         if (audio->in[audio->fill_next].used == 0) {
214                 audqcelp_buffer_refresh(audio);
215         } else {
216                 dprintk("audqcelp_update_pcm_buf_entry: read cannot keep up\n");
217                 audio->buf_refresh = 1;
218         }
219
220         spin_unlock_irqrestore(&audio->dsp_lock, flags);
221         wake_up(&audio->read_wait);
222 }
223
224 static void audplay_dsp_event(void *data, unsigned id, size_t len,
225                               void (*getevent) (void *ptr, size_t len))
226 {
227         struct audio *audio = data;
228         uint32_t msg[28];
229         getevent(msg, sizeof(msg));
230
231         dprintk("audplay_dsp_event: msg_id=%x\n", id);
232
233         switch (id) {
234         case AUDPLAY_MSG_DEC_NEEDS_DATA:
235                 audqcelp_send_data(audio, 1);
236                 break;
237
238         case AUDPLAY_MSG_BUFFER_UPDATE:
239                 audqcelp_update_pcm_buf_entry(audio, msg);
240                 break;
241
242         default:
243                 pr_err("unexpected message from decoder \n");
244         }
245 }
246
247 static void audqcelp_dsp_event(void *private, unsigned id, uint16_t *msg)
248 {
249         struct audio *audio = private;
250
251         switch (id) {
252         case AUDPP_MSG_STATUS_MSG:{
253                         unsigned status = msg[1];
254
255                         switch (status) {
256                         case AUDPP_DEC_STATUS_SLEEP:
257                                 dprintk("decoder status: sleep \n");
258                                 break;
259
260                         case AUDPP_DEC_STATUS_INIT:
261                                 dprintk("decoder status: init \n");
262                                 audpp_cmd_cfg_routing_mode(audio);
263                                 break;
264
265                         case AUDPP_DEC_STATUS_CFG:
266                                 dprintk("decoder status: cfg \n");
267                                 break;
268                         case AUDPP_DEC_STATUS_PLAY:
269                                 dprintk("decoder status: play \n");
270                                 if (audio->pcm_feedback) {
271                                         audqcelp_config_hostpcm(audio);
272                                         audqcelp_buffer_refresh(audio);
273                                 }
274                                 break;
275                         default:
276                                 pr_err("unknown decoder status \n");
277                         }
278                         break;
279                 }
280         case AUDPP_MSG_CFG_MSG:
281                 if (msg[0] == AUDPP_MSG_ENA_ENA) {
282                         dprintk("audqcelp_dsp_event: CFG_MSG ENABLE\n");
283                         auddec_dsp_config(audio, 1);
284                         audio->out_needed = 0;
285                         audio->running = 1;
286                         audpp_set_volume_and_pan(audio->dec_id, audio->volume,
287                                                  0);
288                         audpp_avsync(audio->dec_id, 22050);
289                 } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
290                         dprintk("audqcelp_dsp_event: CFG_MSG DISABLE\n");
291                         audpp_avsync(audio->dec_id, 0);
292                         audio->running = 0;
293                 } else {
294                         pr_err("audqcelp_dsp_event: CFG_MSG %d?\n", msg[0]);
295                 }
296                 break;
297         case AUDPP_MSG_ROUTING_ACK:
298                 dprintk("audqcelp_dsp_event: ROUTING_ACK mode=%d\n", msg[1]);
299                 audpp_cmd_cfg_adec_params(audio);
300                 break;
301         default:
302                 pr_err("audqcelp_dsp_event: UNKNOWN (%d)\n", id);
303         }
304
305 }
306
307 struct msm_adsp_ops audplay_adsp_ops_qcelp = {
308         .event = audplay_dsp_event,
309 };
310
311 #define audplay_send_queue0(audio, cmd, len) \
312         msm_adsp_write(audio->audplay, QDSP_uPAudPlay0BitStreamCtrlQueue, \
313                        cmd, len)
314
315 static int auddec_dsp_config(struct audio *audio, int enable)
316 {
317         audpp_cmd_cfg_dec_type cmd;
318
319         memset(&cmd, 0, sizeof(cmd));
320         cmd.cmd_id = AUDPP_CMD_CFG_DEC_TYPE;
321         if (enable)
322                 cmd.dec0_cfg = AUDPP_CMD_UPDATDE_CFG_DEC |
323                     AUDPP_CMD_ENA_DEC_V | AUDDEC_DEC_QCELP;
324         else
325                 cmd.dec0_cfg = AUDPP_CMD_UPDATDE_CFG_DEC | AUDPP_CMD_DIS_DEC_V;
326
327         return audpp_send_queue1(&cmd, sizeof(cmd));
328 }
329
330 static void audpp_cmd_cfg_adec_params(struct audio *audio)
331 {
332         struct audpp_cmd_cfg_adec_params_v13k cmd;
333
334         memset(&cmd, 0, sizeof(cmd));
335         cmd.common.cmd_id = AUDPP_CMD_CFG_ADEC_PARAMS;
336         cmd.common.length = AUDPP_CMD_CFG_ADEC_PARAMS_V13K_LEN;
337         cmd.common.dec_id = audio->dec_id;
338         cmd.common.input_sampling_frequency = 8000;
339         cmd.stereo_cfg = AUDPP_CMD_PCM_INTF_MONO_V;
340
341         audpp_send_queue2(&cmd, sizeof(cmd));
342 }
343
344 static void audpp_cmd_cfg_routing_mode(struct audio *audio)
345 {
346         struct audpp_cmd_routing_mode cmd;
347         dprintk("audpp_cmd_cfg_routing_mode()\n");
348         memset(&cmd, 0, sizeof(cmd));
349         cmd.cmd_id = AUDPP_CMD_ROUTING_MODE;
350         cmd.object_number = audio->dec_id;
351         if (audio->pcm_feedback)
352                 cmd.routing_mode = ROUTING_MODE_FTRT;
353         else
354                 cmd.routing_mode = ROUTING_MODE_RT;
355         audpp_send_queue1(&cmd, sizeof(cmd));
356 }
357
358 static int audplay_dsp_send_data_avail(struct audio *audio,
359                                        unsigned idx, unsigned len)
360 {
361         audplay_cmd_bitstream_data_avail cmd;
362
363         cmd.cmd_id = AUDPLAY_CMD_BITSTREAM_DATA_AVAIL;
364         cmd.decoder_id = audio->dec_id;
365         cmd.buf_ptr = audio->out[idx].addr;
366         cmd.buf_size = len / 2;
367         cmd.partition_number = 0;
368         return audplay_send_queue0(audio, &cmd, sizeof(cmd));
369 }
370
371 static void audqcelp_buffer_refresh(struct audio *audio)
372 {
373         struct audplay_cmd_buffer_refresh refresh_cmd;
374
375         refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
376         refresh_cmd.num_buffers = 1;
377         refresh_cmd.buf0_address = audio->in[audio->fill_next].addr;
378         refresh_cmd.buf0_length = audio->in[audio->fill_next].size;
379         refresh_cmd.buf_read_count = 0;
380         dprintk("audplay_buffer_fresh: buf0_addr=%x buf0_len=%d\n",
381                 refresh_cmd.buf0_address, refresh_cmd.buf0_length);
382
383         (void)audplay_send_queue0(audio, &refresh_cmd, sizeof(refresh_cmd));
384 }
385
386 static void audqcelp_config_hostpcm(struct audio *audio)
387 {
388         struct audplay_cmd_hpcm_buf_cfg cfg_cmd;
389
390         dprintk("audqcelp_config_hostpcm()\n");
391         cfg_cmd.cmd_id = AUDPLAY_CMD_HPCM_BUF_CFG;
392         cfg_cmd.max_buffers = audio->pcm_buf_count;
393         cfg_cmd.byte_swap = 0;
394         cfg_cmd.hostpcm_config = (0x8000) | (0x4000);
395         cfg_cmd.feedback_frequency = 1;
396         cfg_cmd.partition_number = 0;
397
398         (void)audplay_send_queue0(audio, &cfg_cmd, sizeof(cfg_cmd));
399 }
400
401 static void audqcelp_send_data(struct audio *audio, unsigned needed)
402 {
403         struct buffer *frame;
404         unsigned long flags;
405
406         spin_lock_irqsave(&audio->dsp_lock, flags);
407         if (!audio->running)
408                 goto done;
409
410         if (needed) {
411                 /* We were called from the callback because the DSP
412                  * requested more data.  Note that the DSP does want
413                  * more data, and if a buffer was in-flight, mark it
414                  * as available (since the DSP must now be done with
415                  * it).
416                  */
417                 audio->out_needed = 1;
418                 frame = audio->out + audio->out_tail;
419                 if (frame->used == 0xffffffff) {
420                         dprintk("frame %d free\n", audio->out_tail);
421                         frame->used = 0;
422                         audio->out_tail ^= 1;
423                         wake_up(&audio->write_wait);
424                 }
425         }
426
427         if (audio->out_needed) {
428                 /* If the DSP currently wants data and we have a
429                  * buffer available, we will send it and reset
430                  * the needed flag.  We'll mark the buffer as in-flight
431                  * so that it won't be recycled until the next buffer
432                  * is requested
433                  */
434
435                 frame = audio->out + audio->out_tail;
436                 if (frame->used) {
437                         BUG_ON(frame->used == 0xffffffff);
438                         dprintk("frame %d busy\n", audio->out_tail);
439                         audplay_dsp_send_data_avail(audio, audio->out_tail,
440                                                     frame->used);
441                         frame->used = 0xffffffff;
442                         audio->out_needed = 0;
443                 }
444         }
445  done:
446         spin_unlock_irqrestore(&audio->dsp_lock, flags);
447 }
448
449 /* ------------------- device --------------------- */
450
451 static void audqcelp_flush(struct audio *audio)
452 {
453         audio->out[0].used = 0;
454         audio->out[1].used = 0;
455         audio->out_head = 0;
456         audio->out_tail = 0;
457         audio->stopped = 0;
458 }
459
460 static void audqcelp_flush_pcm_buf(struct audio *audio)
461 {
462         uint8_t index;
463
464         for (index = 0; index < PCM_BUF_MAX_COUNT; index++)
465                 audio->in[index].used = 0;
466
467         audio->read_next = 0;
468         audio->fill_next = 0;
469 }
470
471 static long audqcelp_ioctl(struct file *file, unsigned int cmd,
472                 unsigned long arg)
473 {
474         struct audio *audio = file->private_data;
475         int rc = 0;
476
477         dprintk("audqcelp_ioctl() cmd = %d\n", cmd);
478
479         if (cmd == AUDIO_GET_STATS) {
480                 struct msm_audio_stats stats;
481                 stats.byte_count = audpp_avsync_byte_count(audio->dec_id);
482                 stats.sample_count = audpp_avsync_sample_count(audio->dec_id);
483                 if (copy_to_user((void *)arg, &stats, sizeof(stats)))
484                         return -EFAULT;
485                 return 0;
486         }
487         if (cmd == AUDIO_SET_VOLUME) {
488                 unsigned long flags;
489                 spin_lock_irqsave(&audio->dsp_lock, flags);
490                 audio->volume = arg;
491                 if (audio->running)
492                         audpp_set_volume_and_pan(audio->dec_id, arg, 0);
493                 spin_unlock_irqrestore(&audio->dsp_lock, flags);
494                 return 0;
495         }
496         mutex_lock(&audio->lock);
497         switch (cmd) {
498         case AUDIO_START:
499                 rc = audqcelp_enable(audio);
500                 break;
501         case AUDIO_STOP:
502                 rc = audqcelp_disable(audio);
503                 audio->stopped = 1;
504                 break;
505         case AUDIO_FLUSH:
506                 if (audio->stopped) {
507                         /* Make sure we're stopped and we wake any threads
508                          * that might be blocked holding the write_lock.
509                          * While audio->stopped write threads will always
510                          * exit immediately.
511                          */
512                         wake_up(&audio->write_wait);
513                         mutex_lock(&audio->write_lock);
514                         audqcelp_flush(audio);
515                         mutex_unlock(&audio->write_lock);
516                         wake_up(&audio->read_wait);
517                         mutex_lock(&audio->read_lock);
518                         audqcelp_flush_pcm_buf(audio);
519                         mutex_unlock(&audio->read_lock);
520                         break;
521                 }
522                 break;
523         case AUDIO_SET_CONFIG:
524                 dprintk("AUDIO_SET_CONFIG not applicable \n");
525                 break;
526         case AUDIO_GET_CONFIG:{
527                         struct msm_audio_config config;
528                         config.buffer_size = BUFSZ;
529                         config.buffer_count = BUF_COUNT;
530                         config.sample_rate = 8000;
531                         config.channel_count = 1;
532                         config.unused[0] = 0;
533                         config.unused[1] = 0;
534                         config.unused[2] = 0;
535                         config.unused[3] = 0;
536                         if (copy_to_user((void *)arg, &config,
537                                          sizeof(config)))
538                                 rc = -EFAULT;
539                         else
540                                 rc = 0;
541
542                         break;
543                 }
544         case AUDIO_GET_PCM_CONFIG:{
545                         struct msm_audio_pcm_config config;
546
547                         config.pcm_feedback = 0;
548                         config.buffer_count = PCM_BUF_MAX_COUNT;
549                         config.buffer_size = PCM_BUFSZ_MIN;
550                         if (copy_to_user((void *)arg, &config,
551                                 sizeof(config)))
552                                 rc = -EFAULT;
553                         else
554                                 rc = 0;
555                         break;
556                 }
557         case AUDIO_SET_PCM_CONFIG:{
558                         struct msm_audio_pcm_config config;
559
560                         if (copy_from_user(&config, (void *)arg,
561                                 sizeof(config))) {
562                                 rc = -EFAULT;
563                                 break;
564                         }
565                         if ((config.buffer_count > PCM_BUF_MAX_COUNT) ||
566                                 (config.buffer_count == 1))
567                                 config.buffer_count = PCM_BUF_MAX_COUNT;
568
569                         if (config.buffer_size < PCM_BUFSZ_MIN)
570                                 config.buffer_size = PCM_BUFSZ_MIN;
571
572                         /* Check if pcm feedback is required */
573                         if ((config.pcm_feedback) && (!audio->read_data)) {
574                                 dprintk(
575                                 "audqcelp_ioctl: allocate PCM buf %d\n",
576                                 config.buffer_count * config.buffer_size);
577                                 audio->read_data = dma_alloc_coherent(NULL,
578                                 config.buffer_size * config.buffer_count,
579                                 &audio->read_phys, GFP_KERNEL);
580                                 if (!audio->read_data) {
581                                         pr_err(
582                                         "audqcelp_ioctl: no mem for pcm buf\n"
583                                         );
584                                         rc = -ENOMEM;
585                                 } else {
586                                         uint8_t index;
587                                         uint32_t offset = 0;
588
589                                         audio->pcm_feedback = 1;
590                                         audio->buf_refresh = 0;
591                                         audio->pcm_buf_count =
592                                                 config.buffer_count;
593                                         audio->read_next = 0;
594                                         audio->fill_next = 0;
595
596                                         for (index = 0;
597                                         index < config.buffer_count; index++) {
598                                                 audio->in[index].data =
599                                                 audio->read_data + offset;
600                                                 audio->in[index].addr =
601                                                 audio->read_phys + offset;
602                                                 audio->in[index].size =
603                                                 config.buffer_size;
604                                                 audio->in[index].used = 0;
605                                                 offset += config.buffer_size;
606                                         }
607                                         rc = 0;
608                                 }
609                         } else {
610                                 rc = 0;
611                         }
612                         break;
613                 }
614         case AUDIO_PAUSE:
615                 dprintk("%s: AUDIO_PAUSE %ld\n", __func__, arg);
616                 rc = audpp_pause(audio->dec_id, (int) arg);
617                 break;
618         default:
619                 rc = -EINVAL;
620         }
621         mutex_unlock(&audio->lock);
622         return rc;
623 }
624
625 static ssize_t audqcelp_read(struct file *file, char __user *buf, size_t count,
626                         loff_t *pos)
627 {
628         struct audio *audio = file->private_data;
629         const char __user *start = buf;
630         int rc = 0;
631
632         if (!audio->pcm_feedback)
633                 return 0; /* PCM feedback is not enabled. Nothing to read */
634
635         mutex_lock(&audio->read_lock);
636         dprintk("audqcelp_read() %d \n", count);
637         while (count > 0) {
638                 rc = wait_event_interruptible(audio->read_wait,
639                                 (audio->in[audio->read_next].used > 0) ||
640                                 (audio->stopped));
641                 if (rc < 0)
642                         break;
643
644                 if (audio->stopped) {
645                         rc = -EBUSY;
646                         break;
647                 }
648
649                 if (count < audio->in[audio->read_next].used) {
650                         /* Read must happen in frame boundary. Since driver does
651                         not know frame size, read count must be greater or equal
652                         to size of PCM samples */
653                         dprintk("audqcelp_read:read stop - partial frame\n");
654                         break;
655                 } else {
656                         dprintk("audqcelp_read: read from in[%d]\n",
657                                 audio->read_next);
658                         if (copy_to_user(buf,
659                                 audio->in[audio->read_next].data,
660                                 audio->in[audio->read_next].used)) {
661                                 pr_err("audqcelp_read: invalid addr %x \n",
662                                         (unsigned int)buf);
663                                 rc = -EFAULT;
664                                 break;
665                         }
666                         count -= audio->in[audio->read_next].used;
667                         buf += audio->in[audio->read_next].used;
668                         audio->in[audio->read_next].used = 0;
669                         if ((++audio->read_next) == audio->pcm_buf_count)
670                                 audio->read_next = 0;
671                 }
672         }
673
674         if (audio->buf_refresh) {
675                 audio->buf_refresh = 0;
676                 dprintk("audqcelp_read: kick start pcm feedback again\n");
677                 audqcelp_buffer_refresh(audio);
678         }
679
680         mutex_unlock(&audio->read_lock);
681
682         if (buf > start)
683                 rc = buf - start;
684
685         dprintk("audqcelp_read: read %d bytes\n", rc);
686         return rc;
687 }
688
689 static ssize_t audqcelp_write(struct file *file, const char __user *buf,
690                            size_t count, loff_t *pos)
691 {
692         struct audio *audio = file->private_data;
693         const char __user *start = buf;
694         struct buffer *frame;
695         size_t xfer;
696         int rc = 0;
697
698         if (count & 1)
699                 return -EINVAL;
700         dprintk("audqcelp_write() \n");
701         mutex_lock(&audio->write_lock);
702         while (count > 0) {
703                 frame = audio->out + audio->out_head;
704                 rc = wait_event_interruptible(audio->write_wait,
705                                               (frame->used == 0)
706                                               || (audio->stopped));
707                 dprintk("audqcelp_write() buffer available\n");
708                 if (rc < 0)
709                         break;
710                 if (audio->stopped) {
711                         rc = -EBUSY;
712                         break;
713                 }
714                 xfer = (count > frame->size) ? frame->size : count;
715                 if (copy_from_user(frame->data, buf, xfer)) {
716                         rc = -EFAULT;
717                         break;
718                 }
719
720                 frame->used = xfer;
721                 audio->out_head ^= 1;
722                 count -= xfer;
723                 buf += xfer;
724
725                 audqcelp_send_data(audio, 0);
726
727         }
728         mutex_unlock(&audio->write_lock);
729         if (buf > start)
730                 return buf - start;
731         return rc;
732 }
733
734 static int audqcelp_release(struct inode *inode, struct file *file)
735 {
736         struct audio *audio = file->private_data;
737
738         dprintk("audqcelp_release()\n");
739
740         mutex_lock(&audio->lock);
741         audqcelp_disable(audio);
742         audqcelp_flush(audio);
743         audqcelp_flush_pcm_buf(audio);
744         msm_adsp_put(audio->audplay);
745         audio->audplay = NULL;
746         audio->opened = 0;
747         if (audio->data)
748                 dma_free_coherent(NULL, DMASZ, audio->data, audio->phys);
749         audio->data = NULL;
750         if (audio->read_data) {
751                 dma_free_coherent(NULL,
752                                  audio->in[0].size * audio->pcm_buf_count,
753                                  audio->read_data, audio->read_phys);
754                 audio->read_data = NULL;
755         }
756         audio->pcm_feedback = 0;
757         mutex_unlock(&audio->lock);
758         return 0;
759 }
760
761 static int audqcelp_open(struct inode *inode, struct file *file)
762 {
763         struct audio *audio = &the_qcelp_audio;
764         int rc;
765
766         mutex_lock(&audio->lock);
767
768         if (audio->opened) {
769                 pr_err("audio: busy\n");
770                 rc = -EBUSY;
771                 goto done;
772         }
773
774         audio->data = dma_alloc_coherent(NULL, DMASZ,
775                                          &audio->phys, GFP_KERNEL);
776         if (!audio->data) {
777                 pr_err("audio: could not allocate DMA buffers\n");
778                 rc = -ENOMEM;
779                 goto done;
780         }
781
782         rc = audmgr_open(&audio->audmgr);
783         if (rc)
784                 goto err;
785
786         rc = msm_adsp_get("AUDPLAY0TASK", &audio->audplay,
787                 &audplay_adsp_ops_qcelp, audio);
788         if (rc) {
789                 pr_err("audio: failed to get audplay0 dsp module\n");
790                 audmgr_close(&audio->audmgr);
791                 goto err;
792         }
793
794         audio->dec_id = 0;
795
796         audio->out[0].data = audio->data + 0;
797         audio->out[0].addr = audio->phys + 0;
798         audio->out[0].size = BUFSZ;
799
800         audio->out[1].data = audio->data + BUFSZ;
801         audio->out[1].addr = audio->phys + BUFSZ;
802         audio->out[1].size = BUFSZ;
803
804         audio->volume = 0x2000; /* Q13 1.0 */
805
806         audqcelp_flush(audio);
807
808         file->private_data = audio;
809         audio->opened = 1;
810         rc = 0;
811 done:
812         mutex_unlock(&audio->lock);
813         return rc;
814 err:
815         dma_free_coherent(NULL, DMASZ, audio->data, audio->phys);
816         mutex_unlock(&audio->lock);
817         return rc;
818 }
819
820 static struct file_operations audio_qcelp_fops = {
821         .owner = THIS_MODULE,
822         .open = audqcelp_open,
823         .release = audqcelp_release,
824         .read = audqcelp_read,
825         .write = audqcelp_write,
826         .unlocked_ioctl = audqcelp_ioctl,
827         .llseek = noop_llseek,
828 };
829
830 struct miscdevice audio_qcelp_misc = {
831         .minor = MISC_DYNAMIC_MINOR,
832         .name = "msm_qcelp",
833         .fops = &audio_qcelp_fops,
834 };
835
836 static int __init audqcelp_init(void)
837 {
838         mutex_init(&the_qcelp_audio.lock);
839         mutex_init(&the_qcelp_audio.write_lock);
840         mutex_init(&the_qcelp_audio.read_lock);
841         spin_lock_init(&the_qcelp_audio.dsp_lock);
842         init_waitqueue_head(&the_qcelp_audio.write_wait);
843         init_waitqueue_head(&the_qcelp_audio.read_wait);
844         the_qcelp_audio.read_data = NULL;
845         return misc_register(&audio_qcelp_misc);
846 }
847
848 static void __exit audqcelp_exit(void)
849 {
850         misc_deregister(&audio_qcelp_misc);
851 }
852
853 module_init(audqcelp_init);
854 module_exit(audqcelp_exit);
855
856 MODULE_DESCRIPTION("MSM QCELP 13K driver");
857 MODULE_LICENSE("GPL v2");
858 MODULE_AUTHOR("QUALCOMM");