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