blob: 88f19b70e5b1c0d4d2174d54d6acb4df75af3149 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 * Copyright (C) 2009 Google, Inc.
3 * Copyright (C) 2009 HTC Corporation
4 * Copyright (c) 2009, Code Aurora Forum. All rights reserved.
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_qcp.h>
26#include <mach/msm_qdsp6_audiov2.h>
27#include "dal_audio.h"
28#include "dal_audio_format.h"
29#include <mach/debug_mm.h>
30
31
32struct evrc {
33 struct mutex lock;
34 struct msm_audio_evrc_enc_config cfg;
35 struct msm_audio_stream_config str_cfg;
36 struct audio_client *audio_client;
37};
38
39
40static long q6_evrc_in_ioctl(struct file *file, unsigned int cmd,
41 unsigned long arg)
42{
43 struct evrc *evrc = file->private_data;
44 struct adsp_open_command rpc;
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(&evrc->lock);
56 switch (cmd) {
57 case AUDIO_START:
58 if (evrc->audio_client) {
59 rc = -EBUSY;
60 break;
61 } else {
62 evrc->audio_client = q6audio_open(AUDIO_FLAG_READ,
63 evrc->str_cfg.buffer_size);
64
65 if (!evrc->audio_client) {
66 kfree(evrc);
67 rc = -ENOMEM;
68 break;
69 }
70 }
71
72 tx_clk_freq = 8000;
73
74 memset(&rpc, 0, sizeof(rpc));
75
76 rpc.format_block.standard.format = ADSP_AUDIO_FORMAT_EVRC_FS;
77 rpc.format_block.standard.channels = 1;
78 rpc.format_block.standard.bits_per_sample = 16;
79 rpc.format_block.standard.sampling_rate = 8000;
80 rpc.format_block.standard.is_signed = 1;
81 rpc.format_block.standard.is_interleaved = 0;
82
83 rpc.hdr.opcode = ADSP_AUDIO_IOCTL_CMD_OPEN_READ;
84 rpc.device = ADSP_AUDIO_DEVICE_ID_DEFAULT;
85 rpc.stream_context = ADSP_AUDIO_DEVICE_CONTEXT_RECORD;
86 rpc.buf_max_size = evrc->str_cfg.buffer_size;
87 rpc.config.evrc.min_rate = evrc->cfg.min_bit_rate;
88 rpc.config.evrc.max_rate = evrc->cfg.max_bit_rate;
89
90 q6audio_start(evrc->audio_client, &rpc, sizeof(rpc));
91 break;
92 case AUDIO_STOP:
93 break;
94 case AUDIO_FLUSH:
95 break;
96 case AUDIO_SET_VOLUME:
97 break;
98 case AUDIO_GET_STREAM_CONFIG:
99 if (copy_to_user((void *)arg, &evrc->str_cfg,
100 sizeof(struct msm_audio_stream_config)))
101 rc = -EFAULT;
102 break;
103 case AUDIO_SET_STREAM_CONFIG:
104 if (copy_from_user(&evrc->str_cfg, (void *)arg,
105 sizeof(struct msm_audio_stream_config))) {
106 rc = -EFAULT;
107 break;
108 }
109
110 if (evrc->str_cfg.buffer_size < 23) {
111 pr_err("[%s:%s] Buffer size too small\n", __MM_FILE__,
112 __func__);
113 rc = -EINVAL;
114 break;
115 }
116
117 if (evrc->str_cfg.buffer_count != 2)
118 pr_info("[%s:%s] Buffer count set to 2\n", __MM_FILE__,
119 __func__);
120 break;
121 case AUDIO_SET_EVRC_ENC_CONFIG:
122 if (copy_from_user(&evrc->cfg, (void *) arg,
123 sizeof(struct msm_audio_evrc_enc_config)))
124 rc = -EFAULT;
125
126 if (evrc->cfg.min_bit_rate > 4 || evrc->cfg.min_bit_rate < 1) {
127 pr_err("[%s:%s] invalid min bitrate\n", __MM_FILE__,
128 __func__);
129 rc = -EINVAL;
130 }
131 if (evrc->cfg.max_bit_rate > 4 || evrc->cfg.max_bit_rate < 1) {
132 pr_err("[%s:%s] invalid max bitrate\n", __MM_FILE__,
133 __func__);
134 rc = -EINVAL;
135 }
136 break;
137 case AUDIO_GET_EVRC_ENC_CONFIG:
138 if (copy_to_user((void *) arg, &evrc->cfg,
139 sizeof(struct msm_audio_evrc_enc_config)))
140 rc = -EFAULT;
141 break;
142
143 default:
144 rc = -EINVAL;
145 }
146
147 mutex_unlock(&evrc->lock);
148 return rc;
149}
150
151static int q6_evrc_in_open(struct inode *inode, struct file *file)
152{
153 struct evrc *evrc;
154 evrc = kmalloc(sizeof(struct evrc), GFP_KERNEL);
155 if (evrc == NULL) {
156 pr_err("[%s:%s] Could not allocate memory for evrc driver\n",
157 __MM_FILE__, __func__);
158 return -ENOMEM;
159 }
160
161 mutex_init(&evrc->lock);
162 file->private_data = evrc;
163 evrc->audio_client = NULL;
164 evrc->str_cfg.buffer_size = 23;
165 evrc->str_cfg.buffer_count = 2;
166 evrc->cfg.cdma_rate = CDMA_RATE_FULL;
167 evrc->cfg.min_bit_rate = 1;
168 evrc->cfg.max_bit_rate = 4;
169
170 return 0;
171}
172
173static ssize_t q6_evrc_in_read(struct file *file, char __user *buf,
174 size_t count, loff_t *pos)
175{
176 struct audio_client *ac;
177 struct audio_buffer *ab;
178 const char __user *start = buf;
179 struct evrc *evrc = file->private_data;
180 int xfer = 0;
181 int res;
182
183 mutex_lock(&evrc->lock);
184 ac = evrc->audio_client;
185 if (!ac) {
186 res = -ENODEV;
187 goto fail;
188 }
189 while (count > xfer) {
190 ab = ac->buf + ac->cpu_buf;
191
192 if (ab->used)
193 wait_event(ac->wait, (ab->used == 0));
194
195 xfer = ab->actual_size;
196
197 if (copy_to_user(buf, ab->data, xfer)) {
198 res = -EFAULT;
199 goto fail;
200 }
201
202 buf += xfer;
203 count -= xfer;
204
205 ab->used = 1;
206 q6audio_read(ac, ab);
207 ac->cpu_buf ^= 1;
208 }
209
210 res = buf - start;
211
212fail:
213 mutex_unlock(&evrc->lock);
214
215 return res;
216}
217
218static int q6_evrc_in_release(struct inode *inode, struct file *file)
219{
220 int rc = 0;
221 struct evrc *evrc = file->private_data;
222
223 mutex_lock(&evrc->lock);
224 if (evrc->audio_client)
225 rc = q6audio_close(evrc->audio_client);
226 mutex_unlock(&evrc->lock);
227 kfree(evrc);
228 return rc;
229}
230
231static const struct file_operations q6_evrc_in_fops = {
232 .owner = THIS_MODULE,
233 .open = q6_evrc_in_open,
234 .read = q6_evrc_in_read,
235 .release = q6_evrc_in_release,
236 .unlocked_ioctl = q6_evrc_in_ioctl,
237};
238
239struct miscdevice q6_evrc_in_misc = {
240 .minor = MISC_DYNAMIC_MINOR,
241 .name = "msm_evrc_in",
242 .fops = &q6_evrc_in_fops,
243};
244
245static int __init q6_evrc_in_init(void)
246{
247 return misc_register(&q6_evrc_in_misc);
248}
249
250device_initcall(q6_evrc_in_init);