Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* 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 | |
| 31 | struct 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 | |
| 41 | void audio_client_dump(struct audio_client *ac); |
| 42 | |
| 43 | static 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; |
Asish Bhattacharya | 4a06419 | 2013-10-01 17:15:05 +0530 | [diff] [blame] | 144 | memset(&config, 0, sizeof(config)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 145 | config.buffer_size = pcm->buffer_size; |
| 146 | config.buffer_count = 2; |
| 147 | config.sample_rate = pcm->sample_rate; |
| 148 | config.channel_count = pcm->channel_count; |
| 149 | config.unused[0] = 0; |
| 150 | config.unused[1] = 0; |
| 151 | config.unused[2] = 0; |
| 152 | if (copy_to_user((void*) arg, &config, sizeof(config))) { |
| 153 | rc = -EFAULT; |
| 154 | } |
| 155 | pr_debug("[%s:%s] GET_CONFIG: samplerate = %d, channels = %d\n", |
| 156 | __MM_FILE__, __func__, config.sample_rate, |
| 157 | config.channel_count); |
| 158 | break; |
| 159 | } |
| 160 | default: |
| 161 | rc = -EINVAL; |
| 162 | } |
| 163 | pr_debug("[%s:%s] rc = %d\n", __MM_FILE__, __func__, rc); |
| 164 | return rc; |
| 165 | } |
| 166 | |
| 167 | static int q6_in_open(struct inode *inode, struct file *file) |
| 168 | { |
| 169 | struct pcm *pcm; |
| 170 | |
| 171 | pr_info("[%s:%s] open\n", __MM_FILE__, __func__); |
| 172 | pcm = kzalloc(sizeof(struct pcm), GFP_KERNEL); |
| 173 | |
| 174 | if (!pcm) |
| 175 | return -ENOMEM; |
| 176 | |
| 177 | pcm->channel_count = 1; |
| 178 | pcm->sample_rate = 8000; |
| 179 | pcm->buffer_size = BUFSZ; |
| 180 | pcm->rec_mode = AUDIO_FLAG_READ; |
| 181 | file->private_data = pcm; |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static ssize_t q6_in_read(struct file *file, char __user *buf, |
| 186 | size_t count, loff_t *pos) |
| 187 | { |
| 188 | struct pcm *pcm = file->private_data; |
| 189 | struct audio_client *ac; |
| 190 | struct audio_buffer *ab; |
| 191 | const char __user *start = buf; |
| 192 | int xfer; |
| 193 | int res; |
| 194 | |
| 195 | pr_debug("[%s:%s] count = %d\n", __MM_FILE__, __func__, count); |
| 196 | ac = pcm->ac; |
| 197 | if (!ac) { |
| 198 | res = -ENODEV; |
| 199 | goto fail; |
| 200 | } |
| 201 | while (count > 0) { |
| 202 | ab = ac->buf + ac->cpu_buf; |
| 203 | |
| 204 | if (ab->used) |
| 205 | if (!wait_event_timeout(ac->wait, (ab->used == 0), 5*HZ)) { |
| 206 | audio_client_dump(ac); |
| 207 | pr_err("[%s:%s] timeout. dsp dead?\n", |
| 208 | __MM_FILE__, __func__); |
| 209 | q6audio_dsp_not_responding(); |
| 210 | } |
| 211 | pr_debug("[%s:%s] ab->data = %p, cpu_buf = %d", __MM_FILE__, |
| 212 | __func__, ab->data, ac->cpu_buf); |
| 213 | xfer = count; |
| 214 | if (xfer > ab->size) |
| 215 | xfer = ab->size; |
| 216 | |
| 217 | if (copy_to_user(buf, ab->data, xfer)) { |
| 218 | res = -EFAULT; |
| 219 | goto fail; |
| 220 | } |
| 221 | |
| 222 | buf += xfer; |
| 223 | count -= xfer; |
| 224 | |
| 225 | ab->used = 1; |
| 226 | q6audio_read(ac, ab); |
| 227 | ac->cpu_buf ^= 1; |
| 228 | } |
| 229 | fail: |
| 230 | res = buf - start; |
| 231 | return res; |
| 232 | } |
| 233 | |
| 234 | static int q6_in_release(struct inode *inode, struct file *file) |
| 235 | { |
| 236 | |
| 237 | int rc = 0; |
| 238 | struct pcm *pcm = file->private_data; |
| 239 | if (pcm->ac) |
| 240 | rc = q6audio_close(pcm->ac); |
| 241 | kfree(pcm); |
| 242 | pr_info("[%s:%s] release\n", __MM_FILE__, __func__); |
| 243 | return rc; |
| 244 | } |
| 245 | |
| 246 | static struct file_operations q6_in_fops = { |
| 247 | .owner = THIS_MODULE, |
| 248 | .open = q6_in_open, |
| 249 | .read = q6_in_read, |
| 250 | .release = q6_in_release, |
| 251 | .unlocked_ioctl = q6_in_ioctl, |
| 252 | }; |
| 253 | |
| 254 | struct miscdevice q6_in_misc = { |
| 255 | .minor = MISC_DYNAMIC_MINOR, |
| 256 | .name = "msm_pcm_in", |
| 257 | .fops = &q6_in_fops, |
| 258 | }; |
| 259 | |
| 260 | static int __init q6_in_init(void) { |
| 261 | return misc_register(&q6_in_misc); |
| 262 | } |
| 263 | |
| 264 | device_initcall(q6_in_init); |