blob: c6bddb883eae0b25ef89da062601d8475b73a6ac [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* arch/arm/mach-msm/qdsp6/pcm_in.c
2 *
3 * Copyright (C) 2009 Google, Inc.
4 * Copyright (C) 2009 HTC Corporation
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/slab.h>
18#include <linux/fs.h>
19#include <linux/module.h>
20#include <linux/miscdevice.h>
21#include <linux/mutex.h>
22#include <linux/sched.h>
23#include <linux/wait.h>
24#include <linux/uaccess.h>
25
26#include <linux/msm_audio.h>
27
28#include <mach/msm_qdsp6_audio.h>
29#include <mach/debug_mm.h>
30
31struct pcm {
32 struct audio_client *ac;
33 uint32_t sample_rate;
34 uint32_t channel_count;
35 uint32_t buffer_size;
36 uint32_t rec_mode;
37};
38
39#define BUFSZ (256)
40
41void audio_client_dump(struct audio_client *ac);
42
43static long q6_in_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
44{
45 struct pcm *pcm = file->private_data;
46 int rc = 0;
47
48 switch (cmd) {
49 case AUDIO_SET_VOLUME:
50 pr_debug("[%s:%s] SET_VOLUME\n", __MM_FILE__, __func__);
51 break;
52 case AUDIO_GET_STATS: {
53 struct msm_audio_stats stats;
54 pr_debug("[%s:%s] GET_STATS\n", __MM_FILE__, __func__);
55 memset(&stats, 0, sizeof(stats));
56 if (copy_to_user((void*) arg, &stats, sizeof(stats)))
57 return -EFAULT;
58 return 0;
59 }
60 case AUDIO_START: {
61 uint32_t acdb_id;
62 pr_debug("[%s:%s] AUDIO_START\n", __MM_FILE__, __func__);
63 rc = 0;
64
65 if (arg == 0) {
66 acdb_id = 0;
67 } else if (copy_from_user(&acdb_id, (void*) arg, sizeof(acdb_id))) {
68 rc = -EFAULT;
69 break;
70 }
71
72 if (pcm->ac) {
73 pr_err("[%s:%s] active session already existing\n",
74 __MM_FILE__, __func__);
75 rc = -EBUSY;
76 } else {
77 pcm->ac = q6audio_open_pcm(pcm->buffer_size,
78 pcm->sample_rate, pcm->channel_count,
79 pcm->rec_mode, acdb_id);
80 if (!pcm->ac) {
81 pr_err("[%s:%s] pcm open session failed\n",
82 __MM_FILE__, __func__);
83 rc = -ENOMEM;
84 }
85 }
86 break;
87 }
88 case AUDIO_STOP:
89 pr_debug("[%s:%s] AUDIO_STOP\n", __MM_FILE__, __func__);
90 break;
91 case AUDIO_FLUSH:
92 break;
93 case AUDIO_SET_CONFIG: {
94 struct msm_audio_config config;
95 if (copy_from_user(&config, (void*) arg, sizeof(config))) {
96 rc = -EFAULT;
97 break;
98 }
99 pr_debug("[%s:%s] SET_CONFIG: samplerate = %d, channels = %d\n",
100 __MM_FILE__, __func__, config.sample_rate,
101 config.channel_count);
102 if (!config.channel_count || config.channel_count > 2) {
103 rc = -EINVAL;
104 pr_err("[%s:%s] invalid channelcount %d\n",
105 __MM_FILE__, __func__, config.channel_count);
106 break;
107 }
108 if (config.sample_rate < 8000 || config.sample_rate > 48000) {
109 rc = -EINVAL;
110 pr_err("[%s:%s] invalid samplerate %d\n", __MM_FILE__,
111 __func__, config.sample_rate);
112 break;
113 }
114 if (config.buffer_size < 128 || config.buffer_size > 8192) {
115 rc = -EINVAL;
116 pr_err("[%s:%s] invalid buffsize %d\n", __MM_FILE__,
117 __func__, config.buffer_size);
118 break;
119 }
120
121 pcm->sample_rate = config.sample_rate;
122 pcm->channel_count = config.channel_count;
123 pcm->buffer_size = config.buffer_size;
124 break;
125 }
126 case AUDIO_SET_INCALL: {
127 struct msm_voicerec_mode voicerec_mode;
128 pr_debug("[%s:%s] SET_INCALL\n", __MM_FILE__, __func__);
129 if (copy_from_user(&voicerec_mode, (void *)arg,
130 sizeof(struct msm_voicerec_mode)))
131 return -EFAULT;
132 if (voicerec_mode.rec_mode != AUDIO_FLAG_READ &&
133 voicerec_mode.rec_mode != AUDIO_FLAG_INCALL_MIXED) {
134 pcm->rec_mode = AUDIO_FLAG_READ;
135 pr_err("[%s:%s] invalid rec_mode\n", __MM_FILE__,
136 __func__);
137 rc = -EINVAL;
138 } else
139 pcm->rec_mode = voicerec_mode.rec_mode;
140 break;
141 }
142 case AUDIO_GET_CONFIG: {
143 struct msm_audio_config config;
144 config.buffer_size = pcm->buffer_size;
145 config.buffer_count = 2;
146 config.sample_rate = pcm->sample_rate;
147 config.channel_count = pcm->channel_count;
148 config.unused[0] = 0;
149 config.unused[1] = 0;
150 config.unused[2] = 0;
151 if (copy_to_user((void*) arg, &config, sizeof(config))) {
152 rc = -EFAULT;
153 }
154 pr_debug("[%s:%s] GET_CONFIG: samplerate = %d, channels = %d\n",
155 __MM_FILE__, __func__, config.sample_rate,
156 config.channel_count);
157 break;
158 }
159 default:
160 rc = -EINVAL;
161 }
162 pr_debug("[%s:%s] rc = %d\n", __MM_FILE__, __func__, rc);
163 return rc;
164}
165
166static int q6_in_open(struct inode *inode, struct file *file)
167{
168 struct pcm *pcm;
169
170 pr_info("[%s:%s] open\n", __MM_FILE__, __func__);
171 pcm = kzalloc(sizeof(struct pcm), GFP_KERNEL);
172
173 if (!pcm)
174 return -ENOMEM;
175
176 pcm->channel_count = 1;
177 pcm->sample_rate = 8000;
178 pcm->buffer_size = BUFSZ;
179 pcm->rec_mode = AUDIO_FLAG_READ;
180 file->private_data = pcm;
181 return 0;
182}
183
184static ssize_t q6_in_read(struct file *file, char __user *buf,
185 size_t count, loff_t *pos)
186{
187 struct pcm *pcm = file->private_data;
188 struct audio_client *ac;
189 struct audio_buffer *ab;
190 const char __user *start = buf;
191 int xfer;
192 int res;
193
194 pr_debug("[%s:%s] count = %d\n", __MM_FILE__, __func__, count);
195 ac = pcm->ac;
196 if (!ac) {
197 res = -ENODEV;
198 goto fail;
199 }
200 while (count > 0) {
201 ab = ac->buf + ac->cpu_buf;
202
203 if (ab->used)
204 if (!wait_event_timeout(ac->wait, (ab->used == 0), 5*HZ)) {
205 audio_client_dump(ac);
206 pr_err("[%s:%s] timeout. dsp dead?\n",
207 __MM_FILE__, __func__);
208 q6audio_dsp_not_responding();
209 }
210 pr_debug("[%s:%s] ab->data = %p, cpu_buf = %d", __MM_FILE__,
211 __func__, ab->data, ac->cpu_buf);
212 xfer = count;
213 if (xfer > ab->size)
214 xfer = ab->size;
215
216 if (copy_to_user(buf, ab->data, xfer)) {
217 res = -EFAULT;
218 goto fail;
219 }
220
221 buf += xfer;
222 count -= xfer;
223
224 ab->used = 1;
225 q6audio_read(ac, ab);
226 ac->cpu_buf ^= 1;
227 }
228fail:
229 res = buf - start;
230 return res;
231}
232
233static int q6_in_release(struct inode *inode, struct file *file)
234{
235
236 int rc = 0;
237 struct pcm *pcm = file->private_data;
238 if (pcm->ac)
239 rc = q6audio_close(pcm->ac);
240 kfree(pcm);
241 pr_info("[%s:%s] release\n", __MM_FILE__, __func__);
242 return rc;
243}
244
245static struct file_operations q6_in_fops = {
246 .owner = THIS_MODULE,
247 .open = q6_in_open,
248 .read = q6_in_read,
249 .release = q6_in_release,
250 .unlocked_ioctl = q6_in_ioctl,
251};
252
253struct miscdevice q6_in_misc = {
254 .minor = MISC_DYNAMIC_MINOR,
255 .name = "msm_pcm_in",
256 .fops = &q6_in_fops,
257};
258
259static int __init q6_in_init(void) {
260 return misc_register(&q6_in_misc);
261}
262
263device_initcall(q6_in_init);