/arch/arm/mach-fsm/qdsp6/pcm_out.c
C | 242 lines | 199 code | 28 blank | 15 comment | 32 complexity | 8681c9cc15d28d3200a483a53d0d6517 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
1/* arch/arm/mach-msm/qdsp6/pcm_out.c
2 *
3 * Copyright (C) 2009 Google, Inc.
4 * Author: Brian Swetland <swetland@google.com>
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17#include <linux/fs.h>
18#include <linux/module.h>
19#include <linux/miscdevice.h>
20#include <linux/mutex.h>
21#include <linux/sched.h>
22#include <linux/wait.h>
23#include <linux/uaccess.h>
24
25#include <linux/msm_audio.h>
26
27#include <mach/msm_qdsp6_audio.h>
28#include <mach/debug_mm.h>
29
30void audio_client_dump(struct audio_client *ac);
31
32#define BUFSZ (3072)
33
34struct pcm {
35 struct mutex lock;
36 struct audio_client *ac;
37 uint32_t sample_rate;
38 uint32_t channel_count;
39 size_t buffer_size;
40};
41
42static long pcm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
43{
44 struct pcm *pcm = file->private_data;
45 int rc = 0;
46
47 if (cmd == AUDIO_GET_STATS) {
48 struct msm_audio_stats stats;
49 memset(&stats, 0, sizeof(stats));
50 if (copy_to_user((void*) arg, &stats, sizeof(stats)))
51 return -EFAULT;
52 return 0;
53 }
54
55 mutex_lock(&pcm->lock);
56 switch (cmd) {
57 case AUDIO_SET_VOLUME: {
58 int vol;
59 if (copy_from_user(&vol, (void*) arg, sizeof(vol))) {
60 rc = -EFAULT;
61 break;
62 }
63 rc = q6audio_set_stream_volume(pcm->ac, vol);
64 break;
65 }
66 case AUDIO_START: {
67 uint32_t acdb_id;
68 if (arg == 0) {
69 acdb_id = 0;
70 } else if (copy_from_user(&acdb_id, (void*) arg, sizeof(acdb_id))) {
71 pr_info("[%s:%s] copy acdb_id from user failed\n",
72 __MM_FILE__, __func__);
73 rc = -EFAULT;
74 break;
75 }
76 if (pcm->ac) {
77 rc = -EBUSY;
78 } else {
79 pcm->ac = q6audio_open_pcm(pcm->buffer_size,
80 pcm->sample_rate,
81 pcm->channel_count,
82 AUDIO_FLAG_WRITE, acdb_id);
83 if (!pcm->ac)
84 rc = -ENOMEM;
85 }
86 break;
87 }
88 case AUDIO_STOP:
89 break;
90 case AUDIO_FLUSH:
91 break;
92 case AUDIO_SET_CONFIG: {
93 struct msm_audio_config config;
94 if (pcm->ac) {
95 rc = -EBUSY;
96 break;
97 }
98 if (copy_from_user(&config, (void*) arg, sizeof(config))) {
99 rc = -EFAULT;
100 break;
101 }
102 if (config.channel_count < 1 || config.channel_count > 2) {
103 rc = -EINVAL;
104 break;
105 }
106 if (config.sample_rate < 8000 || config.sample_rate > 48000) {
107 rc = -EINVAL;
108 break;
109 }
110 if (config.buffer_size < 128 || config.buffer_size > 8192) {
111 rc = -EINVAL;
112 break;
113 }
114 pcm->sample_rate = config.sample_rate;
115 pcm->channel_count = config.channel_count;
116 pcm->buffer_size = config.buffer_size;
117 break;
118 }
119 case AUDIO_GET_CONFIG: {
120 struct msm_audio_config config;
121 config.buffer_size = pcm->buffer_size;
122 config.buffer_count = 2;
123 config.sample_rate = pcm->sample_rate;
124 config.channel_count = pcm->channel_count;
125 config.unused[0] = 0;
126 config.unused[1] = 0;
127 config.unused[2] = 0;
128 if (copy_to_user((void*) arg, &config, sizeof(config))) {
129 rc = -EFAULT;
130 }
131 break;
132 }
133 case AUDIO_SET_EQ: {
134 struct msm_audio_eq_stream_config eq_config;
135 if (copy_from_user(&eq_config, (void *) arg,
136 sizeof(eq_config))) {
137 rc = -EFAULT;
138 break;
139 }
140 rc = q6audio_set_stream_eq_pcm(pcm->ac, (void *) &eq_config);
141 break;
142 }
143 default:
144 rc = -EINVAL;
145 }
146 mutex_unlock(&pcm->lock);
147 return rc;
148}
149
150static int pcm_open(struct inode *inode, struct file *file)
151{
152 struct pcm *pcm;
153
154 pr_info("[%s:%s] open\n", __MM_FILE__, __func__);
155 pcm = kzalloc(sizeof(struct pcm), GFP_KERNEL);
156
157 if (!pcm)
158 return -ENOMEM;
159
160 mutex_init(&pcm->lock);
161 pcm->channel_count = 2;
162 pcm->sample_rate = 44100;
163 pcm->buffer_size = BUFSZ;
164 file->private_data = pcm;
165 return 0;
166}
167
168static ssize_t pcm_write(struct file *file, const char __user *buf,
169 size_t count, loff_t *pos)
170{
171 struct pcm *pcm = file->private_data;
172 struct audio_client *ac;
173 struct audio_buffer *ab;
174 const char __user *start = buf;
175 int xfer;
176
177 if (!pcm->ac)
178 pcm_ioctl(file, AUDIO_START, 0);
179
180 ac = pcm->ac;
181 if (!ac)
182 return -ENODEV;
183
184 while (count > 0) {
185 ab = ac->buf + ac->cpu_buf;
186
187 if (ab->used)
188 if (!wait_event_timeout(ac->wait, (ab->used == 0), 5*HZ)) {
189 audio_client_dump(ac);
190 pr_err("[%s:%s] timeout. dsp dead?\n",
191 __MM_FILE__, __func__);
192 q6audio_dsp_not_responding();
193 }
194
195 xfer = count;
196 if (xfer > ab->size)
197 xfer = ab->size;
198
199 if (copy_from_user(ab->data, buf, xfer))
200 return -EFAULT;
201
202 buf += xfer;
203 count -= xfer;
204
205 ab->used = 1;
206 ab->actual_size = xfer;
207 q6audio_write(ac, ab);
208 ac->cpu_buf ^= 1;
209 }
210
211 return buf - start;
212}
213
214static int pcm_release(struct inode *inode, struct file *file)
215{
216 struct pcm *pcm = file->private_data;
217 if (pcm->ac)
218 q6audio_close(pcm->ac);
219 kfree(pcm);
220 pr_info("[%s:%s] release\n", __MM_FILE__, __func__);
221 return 0;
222}
223
224static struct file_operations pcm_fops = {
225 .owner = THIS_MODULE,
226 .open = pcm_open,
227 .write = pcm_write,
228 .release = pcm_release,
229 .unlocked_ioctl = pcm_ioctl,
230};
231
232struct miscdevice pcm_misc = {
233 .minor = MISC_DYNAMIC_MINOR,
234 .name = "msm_pcm_out",
235 .fops = &pcm_fops,
236};
237
238static int __init pcm_init(void) {
239 return misc_register(&pcm_misc);
240}
241
242device_initcall(pcm_init);