blob: 1b5dd949f7f272cd615f94dc70d860f57c5b5cd1 [file] [log] [blame]
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301/* amrnb audio output device
2 *
3 * Copyright (C) 2008 Google, Inc.
4 * Copyright (C) 2008 HTC Corporation
5 * Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18#include <linux/types.h>
19#include <linux/compat.h>
20#include "audio_utils_aio.h"
21
22static struct miscdevice audio_amrnb_misc;
23static struct ws_mgr audio_amrnb_ws_mgr;
24
25#ifdef CONFIG_DEBUG_FS
26static const struct file_operations audio_amrnb_debug_fops = {
27 .read = audio_aio_debug_read,
28 .open = audio_aio_debug_open,
29};
30#endif
31
32static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
33{
34 struct q6audio_aio *audio = file->private_data;
35 int rc = 0;
36
37 switch (cmd) {
38 case AUDIO_START: {
39 pr_debug("%s[%pK]: AUDIO_START session_id[%d]\n", __func__,
40 audio, audio->ac->session);
41 if (audio->feedback == NON_TUNNEL_MODE) {
42 /* Configure PCM output block */
43 rc = q6asm_enc_cfg_blk_pcm(audio->ac,
44 audio->pcm_cfg.sample_rate,
45 audio->pcm_cfg.channel_count);
46 if (rc < 0) {
47 pr_err("pcm output block config failed\n");
48 break;
49 }
50 }
51
52 rc = audio_aio_enable(audio);
53 audio->eos_rsp = 0;
54 audio->eos_flag = 0;
55 if (!rc) {
56 audio->enabled = 1;
57 } else {
58 audio->enabled = 0;
59 pr_err("Audio Start procedure failed rc=%d\n", rc);
60 break;
61 }
62 pr_debug("AUDIO_START success enable[%d]\n", audio->enabled);
63 if (audio->stopped == 1)
64 audio->stopped = 0;
65 break;
66 }
67 default:
68 pr_debug("%s[%pK]: Calling utils ioctl\n", __func__, audio);
69 rc = audio->codec_ioctl(file, cmd, arg);
70 }
71 return rc;
72}
73
74static long audio_compat_ioctl(struct file *file, unsigned int cmd,
75 unsigned long arg)
76{
77 struct q6audio_aio *audio = file->private_data;
78 int rc = 0;
79
80 switch (cmd) {
81 case AUDIO_START: {
82 pr_debug("%s[%pK]: AUDIO_START session_id[%d]\n", __func__,
83 audio, audio->ac->session);
84 if (audio->feedback == NON_TUNNEL_MODE) {
85 /* Configure PCM output block */
86 rc = q6asm_enc_cfg_blk_pcm(audio->ac,
87 audio->pcm_cfg.sample_rate,
88 audio->pcm_cfg.channel_count);
89 if (rc < 0) {
90 pr_err("%s: pcm output block config failed rc=%d\n",
91 __func__, rc);
92 break;
93 }
94 }
95
96 rc = audio_aio_enable(audio);
97 audio->eos_rsp = 0;
98 audio->eos_flag = 0;
99 if (!rc) {
100 audio->enabled = 1;
101 } else {
102 audio->enabled = 0;
103 pr_err("%s: Audio Start procedure failed rc=%d\n",
104 __func__, rc);
105 break;
106 }
107 pr_debug("AUDIO_START success enable[%d]\n", audio->enabled);
108 if (audio->stopped == 1)
109 audio->stopped = 0;
110 break;
111 }
112 default:
113 pr_debug("%s[%pK]: Calling compat ioctl\n", __func__, audio);
114 rc = audio->codec_compat_ioctl(file, cmd, arg);
115 }
116 return rc;
117}
118
119
120static int audio_open(struct inode *inode, struct file *file)
121{
122 struct q6audio_aio *audio = NULL;
123 int rc = 0;
124
125#ifdef CONFIG_DEBUG_FS
126 /* 4 bytes represents decoder number, 1 byte for terminate string */
127 char name[sizeof "msm_amrnb_" + 5];
128#endif
129 audio = kzalloc(sizeof(struct q6audio_aio), GFP_KERNEL);
130
131 if (audio == NULL)
132 return -ENOMEM;
133
134 audio->pcm_cfg.buffer_size = PCM_BUFSZ_MIN;
135 audio->miscdevice = &audio_amrnb_misc;
136 audio->wakelock_voted = false;
137 audio->audio_ws_mgr = &audio_amrnb_ws_mgr;
138
139 audio->ac = q6asm_audio_client_alloc((app_cb) q6_audio_cb,
140 (void *)audio);
141
142 if (!audio->ac) {
143 pr_err("Could not allocate memory for audio client\n");
144 kfree(audio);
145 return -ENOMEM;
146 }
147 rc = audio_aio_open(audio, file);
148 if (rc < 0) {
149 pr_err("%s: audio_aio_open rc=%d\n",
150 __func__, rc);
151 goto fail;
152 }
153 /* open in T/NT mode */
154 if ((file->f_mode & FMODE_WRITE) && (file->f_mode & FMODE_READ)) {
155 rc = q6asm_open_read_write(audio->ac, FORMAT_LINEAR_PCM,
156 FORMAT_AMRNB);
157 if (rc < 0) {
158 pr_err("NT mode Open failed rc=%d\n", rc);
159 rc = -ENODEV;
160 goto fail;
161 }
162 audio->feedback = NON_TUNNEL_MODE;
163 audio->buf_cfg.frames_per_buf = 0x01;
164 audio->buf_cfg.meta_info_enable = 0x01;
165 } else if ((file->f_mode & FMODE_WRITE) &&
166 !(file->f_mode & FMODE_READ)) {
167 rc = q6asm_open_write(audio->ac, FORMAT_AMRNB);
168 if (rc < 0) {
169 pr_err("T mode Open failed rc=%d\n", rc);
170 rc = -ENODEV;
171 goto fail;
172 }
173 audio->feedback = TUNNEL_MODE;
174 audio->buf_cfg.meta_info_enable = 0x00;
175 } else {
176 pr_err("Not supported mode\n");
177 rc = -EACCES;
178 goto fail;
179 }
180
181#ifdef CONFIG_DEBUG_FS
182 snprintf(name, sizeof(name), "msm_amrnb_%04x", audio->ac->session);
183 audio->dentry = debugfs_create_file(name, S_IFREG | 0444,
184 NULL, (void *)audio,
185 &audio_amrnb_debug_fops);
186
187 if (IS_ERR(audio->dentry))
188 pr_debug("debugfs_create_file failed\n");
189#endif
190 pr_info("%s:amrnb decoder open success, session_id = %d\n", __func__,
191 audio->ac->session);
192 return rc;
193fail:
194 q6asm_audio_client_free(audio->ac);
195 kfree(audio);
196 return rc;
197}
198
199static const struct file_operations audio_amrnb_fops = {
200 .owner = THIS_MODULE,
201 .open = audio_open,
202 .release = audio_aio_release,
203 .unlocked_ioctl = audio_ioctl,
204 .fsync = audio_aio_fsync,
205 .compat_ioctl = audio_compat_ioctl,
206};
207
208static struct miscdevice audio_amrnb_misc = {
209 .minor = MISC_DYNAMIC_MINOR,
210 .name = "msm_amrnb",
211 .fops = &audio_amrnb_fops,
212};
213
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530214int __init audio_amrnb_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530215{
216 int ret = misc_register(&audio_amrnb_misc);
217
218 if (ret == 0)
219 device_init_wakeup(audio_amrnb_misc.this_device, true);
220 audio_amrnb_ws_mgr.ref_cnt = 0;
221 mutex_init(&audio_amrnb_ws_mgr.ws_lock);
222
223 return ret;
224}
225
Asish Bhattacharya5faacb32017-12-04 17:23:15 +0530226void audio_amrnb_exit(void)
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530227{
228 mutex_destroy(&audio_amrnb_ws_mgr.ws_lock);
229 misc_deregister(&audio_amrnb_misc);
230}