blob: ef566c97cf2991202a359d5afd9197df8a0fcf6c [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 * Copyright (C) 2009 Google, Inc.
3 * Copyright (C) 2009 HTC Corporation
Duy Truong790f06d2013-02-13 16:38:12 -08004 * Copyright (c) 2009, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07005 *
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_aac.h>
26
27#include <mach/msm_qdsp6_audiov2.h>
28#include "dal_audio.h"
29#include "dal_audio_format.h"
30
31struct aac {
32 struct mutex lock;
33 struct msm_audio_aac_enc_config cfg;
34 struct msm_audio_stream_config str_cfg;
35 struct audio_client *audio_client;
36};
37
38static long q6_aac_in_ioctl(struct file *file,
39 unsigned int cmd, unsigned long arg)
40{
41 struct aac *aac = file->private_data;
42 struct adsp_open_command rpc;
43
44 int sample_rate;
45 int audio_object_type;
46 int index = sizeof(u32);
47 int rc = 0;
48 u32 *aac_type = NULL;
49
50
51 mutex_lock(&aac->lock);
52 switch (cmd) {
53
54 case AUDIO_START:
55 if (aac->audio_client) {
56 rc = -EBUSY;
57 break;
58 } else {
59 tx_clk_freq = 48000;
60 aac->audio_client = q6audio_open(AUDIO_FLAG_READ,
61 aac->str_cfg.buffer_size);
62
63 if (aac->audio_client < 0) {
64
65 tx_clk_freq = 8000;
66 rc = -ENOMEM;
67 break;
68 }
69 }
70 memset(&rpc, 0, sizeof(rpc));
71
72 rpc.format_block.binary.format = ADSP_AUDIO_FORMAT_MPEG4_AAC;
73 /* only 48k sample rate is supported */
74 sample_rate = 3;
75
76 /* AAC OBJECT LC */
77 audio_object_type = 2;
78
79 aac_type = (u32 *)rpc.format_block.binary.data;
80 switch (aac->cfg.stream_format) {
81
82 case AUDIO_AAC_FORMAT_ADTS:
83 /* AAC Encoder expect MPEG4_ADTS media type */
84 *aac_type = ADSP_AUDIO_AAC_MPEG4_ADTS;
85 break;
86 case AUDIO_AAC_FORMAT_RAW:
87 /* for ADIF recording */
88 *aac_type = ADSP_AUDIO_AAC_RAW;
89 break;
90 }
91
92 rpc.format_block.binary.data[index++] = (u8)(
93 ((audio_object_type & 0x1F) << 3) |
94 ((sample_rate >> 1) & 0x7));
95 rpc.format_block.binary.data[index] = (u8)(
96 ((sample_rate & 0x1) << 7) |
97 ((aac->cfg.channels & 0x7) << 3));
98
99 rpc.format_block.binary.num_bytes = index + 1;
100 rpc.hdr.opcode = ADSP_AUDIO_IOCTL_CMD_OPEN_READ;
101 rpc.device = ADSP_AUDIO_DEVICE_ID_DEFAULT;
102 rpc.stream_context = ADSP_AUDIO_DEVICE_CONTEXT_RECORD;
103 rpc.buf_max_size = aac->str_cfg.buffer_size;
104 rpc.config.aac.bit_rate = aac->cfg.bit_rate;
105 rpc.config.aac.encoder_mode = ADSP_AUDIO_ENC_AAC_LC_ONLY_MODE;
106 q6audio_start(aac->audio_client, &rpc, sizeof(rpc));
107 break;
108 case AUDIO_STOP:
109 break;
110 case AUDIO_FLUSH:
111 break;
112 case AUDIO_SET_VOLUME:
113 break;
114 case AUDIO_GET_STREAM_CONFIG:
115 if (copy_to_user((void *)arg, &aac->str_cfg,
116 sizeof(struct msm_audio_stream_config)))
117 rc = -EFAULT;
118 break;
119 case AUDIO_SET_STREAM_CONFIG:
120 if (copy_from_user(&aac->str_cfg, (void *)arg,
121 sizeof(struct msm_audio_stream_config))) {
122 rc = -EFAULT;
123 break;
124 }
125 if (aac->str_cfg.buffer_size < 519) {
126 pr_err("Buffer size too small\n");
127 rc = -EINVAL;
128 break;
129 }
130 if (aac->str_cfg.buffer_count != 2)
131 pr_info("Buffer count set to 2\n");
132
133 break;
134 case AUDIO_SET_AAC_ENC_CONFIG:
135 if (copy_from_user(&aac->cfg, (void *) arg,
136 sizeof(struct msm_audio_aac_enc_config))) {
137 rc = -EFAULT;
138 }
139 if (aac->cfg.channels != 1) {
140 pr_err("only mono is supported\n");
141 rc = -EINVAL;
142 }
143 if (aac->cfg.sample_rate != 48000) {
144 pr_err("only 48KHz is supported\n");
145 rc = -EINVAL;
146 }
147 if (aac->cfg.stream_format != AUDIO_AAC_FORMAT_RAW &&
148 aac->cfg.stream_format != AUDIO_AAC_FORMAT_ADTS) {
149 pr_err("unsupported AAC format\n");
150 rc = -EINVAL;
151 }
152 break;
153 case AUDIO_GET_AAC_ENC_CONFIG:
154 if (copy_to_user((void *) arg, &aac->cfg,
155 sizeof(struct msm_audio_aac_enc_config))) {
156 rc = -EFAULT;
157 }
158 break;
159 default:
160 rc = -EINVAL;
161 }
162
163 mutex_unlock(&aac->lock);
164 return rc;
165}
166
167static int q6_aac_in_open(struct inode *inode, struct file *file)
168{
169
170 struct aac *aac;
171 aac = kmalloc(sizeof(struct aac), GFP_KERNEL);
172 if (aac == NULL) {
173 pr_err("Could not allocate memory for aac driver\n");
174 return -ENOMEM;
175 }
176
177 mutex_init(&aac->lock);
178 file->private_data = aac;
179 aac->audio_client = NULL;
180 aac->str_cfg.buffer_size = 519;
181 aac->str_cfg.buffer_count = 2;
182 aac->cfg.channels = 1;
183 aac->cfg.bit_rate = 192000;
184 aac->cfg.stream_format = AUDIO_AAC_FORMAT_ADTS;
185 aac->cfg.sample_rate = 48000;
186
187 return 0;
188}
189
190static ssize_t q6_aac_in_read(struct file *file, char __user *buf,
191 size_t count, loff_t *pos)
192{
193 struct audio_client *ac;
194 struct audio_buffer *ab;
195 const char __user *start = buf;
196 struct aac *aac = file->private_data;
197 int xfer = 0;
198 int res;
199
200 mutex_lock(&aac->lock);
201 ac = aac->audio_client;
202 if (!ac) {
203 res = -ENODEV;
204 goto fail;
205 }
206 while (count > xfer) {
207 ab = ac->buf + ac->cpu_buf;
208
209 if (ab->used)
210 wait_event(ac->wait, (ab->used == 0));
211
212 xfer = ab->actual_size;
213
214 if (copy_to_user(buf, ab->data, xfer)) {
215 res = -EFAULT;
216 goto fail;
217 }
218
219 buf += xfer;
220 count -= xfer;
221
222 ab->used = 1;
223 q6audio_read(ac, ab);
224 ac->cpu_buf ^= 1;
225 }
226 res = buf - start;
227fail:
228 mutex_unlock(&aac->lock);
229
230 return res;
231}
232
233static int q6_aac_in_release(struct inode *inode, struct file *file)
234{
235 int rc = 0;
236 struct aac *aac = file->private_data;
237
238 mutex_lock(&aac->lock);
239 if (aac->audio_client)
240 rc = q6audio_close(aac->audio_client);
241 mutex_unlock(&aac->lock);
242 kfree(aac);
243 tx_clk_freq = 8000;
244 return rc;
245}
246
247static const struct file_operations q6_aac_in_fops = {
248 .owner = THIS_MODULE,
249 .open = q6_aac_in_open,
250 .read = q6_aac_in_read,
251 .release = q6_aac_in_release,
252 .unlocked_ioctl = q6_aac_in_ioctl,
253};
254
255struct miscdevice q6_aac_in_misc = {
256 .minor = MISC_DYNAMIC_MINOR,
257 .name = "msm_aac_in",
258 .fops = &q6_aac_in_fops,
259};
260
261static int __init q6_aac_in_init(void)
262{
263 return misc_register(&q6_aac_in_misc);
264}
265
266device_initcall(q6_aac_in_init);