]>
Commit | Line | Data |
---|---|---|
caff4cae IM |
1 | /* arch/arm/mach-msm/qdsp5/audio_out.c |
2 | * | |
3 | * pcm audio output device | |
4 | * | |
5 | * Copyright (C) 2008 Google, Inc. | |
6 | * Copyright (C) 2008 HTC Corporation | |
7 | * | |
8 | * This software is licensed under the terms of the GNU General Public | |
9 | * License version 2, as published by the Free Software Foundation, and | |
10 | * may be copied, distributed, and modified under those terms. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | */ | |
18 | ||
19 | #include <linux/module.h> | |
20 | #include <linux/fs.h> | |
21 | #include <linux/miscdevice.h> | |
22 | #include <linux/uaccess.h> | |
23 | #include <linux/kthread.h> | |
24 | #include <linux/wait.h> | |
25 | #include <linux/dma-mapping.h> | |
26 | #include <linux/debugfs.h> | |
27 | #include <linux/delay.h> | |
28 | #include <linux/wakelock.h> | |
29 | ||
30 | #include <linux/msm_audio.h> | |
31 | ||
32 | #include <asm/atomic.h> | |
33 | #include <asm/ioctls.h> | |
34 | #include <mach/msm_adsp.h> | |
35 | ||
36 | #include "audmgr.h" | |
37 | ||
38 | #include <mach/qdsp5/qdsp5audppcmdi.h> | |
39 | #include <mach/qdsp5/qdsp5audppmsg.h> | |
40 | ||
caff4cae IM |
41 | #include "evlog.h" |
42 | ||
43 | #define LOG_AUDIO_EVENTS 1 | |
44 | #define LOG_AUDIO_FAULTS 0 | |
45 | ||
46 | enum { | |
47 | EV_NULL, | |
48 | EV_OPEN, | |
49 | EV_WRITE, | |
50 | EV_RETURN, | |
51 | EV_IOCTL, | |
52 | EV_WRITE_WAIT, | |
53 | EV_WAIT_EVENT, | |
54 | EV_FILL_BUFFER, | |
55 | EV_SEND_BUFFER, | |
56 | EV_DSP_EVENT, | |
57 | EV_ENABLE, | |
58 | }; | |
59 | ||
60 | #if (LOG_AUDIO_EVENTS != 1) | |
61 | static inline void LOG(unsigned id, unsigned arg) {} | |
62 | #else | |
63 | static const char *pcm_log_strings[] = { | |
64 | "NULL", | |
65 | "OPEN", | |
66 | "WRITE", | |
67 | "RETURN", | |
68 | "IOCTL", | |
69 | "WRITE_WAIT", | |
70 | "WAIT_EVENT", | |
71 | "FILL_BUFFER", | |
72 | "SEND_BUFFER", | |
73 | "DSP_EVENT", | |
74 | "ENABLE", | |
75 | }; | |
76 | ||
77 | DECLARE_LOG(pcm_log, 64, pcm_log_strings); | |
78 | ||
79 | static int __init _pcm_log_init(void) | |
80 | { | |
81 | return ev_log_init(&pcm_log); | |
82 | } | |
83 | module_init(_pcm_log_init); | |
84 | ||
85 | #define LOG(id,arg) ev_log_write(&pcm_log, id, arg) | |
86 | #endif | |
87 | ||
88 | ||
89 | ||
90 | ||
91 | ||
92 | #define BUFSZ (960 * 5) | |
93 | #define DMASZ (BUFSZ * 2) | |
94 | ||
95 | #define AUDPP_CMD_CFG_OBJ_UPDATE 0x8000 | |
96 | #define AUDPP_CMD_EQ_FLAG_DIS 0x0000 | |
97 | #define AUDPP_CMD_EQ_FLAG_ENA -1 | |
98 | #define AUDPP_CMD_IIR_FLAG_DIS 0x0000 | |
99 | #define AUDPP_CMD_IIR_FLAG_ENA -1 | |
100 | ||
101 | #define AUDPP_CMD_IIR_TUNING_FILTER 1 | |
102 | #define AUDPP_CMD_EQUALIZER 2 | |
103 | #define AUDPP_CMD_ADRC 3 | |
104 | ||
105 | #define ADRC_ENABLE 0x0001 | |
106 | #define EQ_ENABLE 0x0002 | |
107 | #define IIR_ENABLE 0x0004 | |
108 | ||
109 | struct adrc_filter { | |
110 | uint16_t compression_th; | |
111 | uint16_t compression_slope; | |
112 | uint16_t rms_time; | |
113 | uint16_t attack_const_lsw; | |
114 | uint16_t attack_const_msw; | |
115 | uint16_t release_const_lsw; | |
116 | uint16_t release_const_msw; | |
117 | uint16_t adrc_system_delay; | |
118 | }; | |
119 | ||
120 | struct eqalizer { | |
121 | uint16_t num_bands; | |
122 | uint16_t eq_params[132]; | |
123 | }; | |
124 | ||
125 | struct rx_iir_filter { | |
126 | uint16_t num_bands; | |
127 | uint16_t iir_params[48]; | |
128 | }; | |
129 | ||
130 | typedef struct { | |
131 | audpp_cmd_cfg_object_params_common common; | |
132 | uint16_t eq_flag; | |
133 | uint16_t num_bands; | |
134 | uint16_t eq_params[132]; | |
135 | } audpp_cmd_cfg_object_params_eq; | |
136 | ||
137 | typedef struct { | |
138 | audpp_cmd_cfg_object_params_common common; | |
139 | uint16_t active_flag; | |
140 | uint16_t num_bands; | |
141 | uint16_t iir_params[48]; | |
142 | } audpp_cmd_cfg_object_params_rx_iir; | |
143 | ||
144 | struct buffer { | |
145 | void *data; | |
146 | unsigned size; | |
147 | unsigned used; | |
148 | unsigned addr; | |
149 | }; | |
150 | ||
151 | struct audio { | |
152 | struct buffer out[2]; | |
153 | ||
154 | spinlock_t dsp_lock; | |
155 | ||
156 | uint8_t out_head; | |
157 | uint8_t out_tail; | |
158 | uint8_t out_needed; /* number of buffers the dsp is waiting for */ | |
159 | ||
160 | atomic_t out_bytes; | |
161 | ||
162 | struct mutex lock; | |
163 | struct mutex write_lock; | |
164 | wait_queue_head_t wait; | |
165 | ||
166 | /* configuration to use on next enable */ | |
167 | uint32_t out_sample_rate; | |
168 | uint32_t out_channel_mode; | |
169 | uint32_t out_weight; | |
170 | uint32_t out_buffer_size; | |
171 | ||
172 | struct audmgr audmgr; | |
173 | ||
174 | /* data allocated for various buffers */ | |
175 | char *data; | |
176 | dma_addr_t phys; | |
177 | ||
178 | int opened; | |
179 | int enabled; | |
180 | int running; | |
181 | int stopped; /* set when stopped, cleared on flush */ | |
182 | unsigned volume; | |
183 | ||
184 | struct wake_lock wakelock; | |
185 | struct wake_lock idlelock; | |
186 | ||
187 | int adrc_enable; | |
188 | struct adrc_filter adrc; | |
189 | ||
190 | int eq_enable; | |
191 | struct eqalizer eq; | |
192 | ||
193 | int rx_iir_enable; | |
194 | struct rx_iir_filter iir; | |
195 | }; | |
196 | ||
197 | static void audio_prevent_sleep(struct audio *audio) | |
198 | { | |
199 | printk(KERN_INFO "++++++++++++++++++++++++++++++\n"); | |
200 | wake_lock(&audio->wakelock); | |
201 | wake_lock(&audio->idlelock); | |
202 | } | |
203 | ||
204 | static void audio_allow_sleep(struct audio *audio) | |
205 | { | |
206 | wake_unlock(&audio->wakelock); | |
207 | wake_unlock(&audio->idlelock); | |
208 | printk(KERN_INFO "------------------------------\n"); | |
209 | } | |
210 | ||
211 | static int audio_dsp_out_enable(struct audio *audio, int yes); | |
212 | static int audio_dsp_send_buffer(struct audio *audio, unsigned id, unsigned len); | |
213 | static int audio_dsp_set_adrc(struct audio *audio); | |
214 | static int audio_dsp_set_eq(struct audio *audio); | |
215 | static int audio_dsp_set_rx_iir(struct audio *audio); | |
216 | ||
217 | static void audio_dsp_event(void *private, unsigned id, uint16_t *msg); | |
218 | ||
219 | /* must be called with audio->lock held */ | |
220 | static int audio_enable(struct audio *audio) | |
221 | { | |
222 | struct audmgr_config cfg; | |
223 | int rc; | |
224 | ||
225 | pr_info("audio_enable()\n"); | |
226 | ||
227 | if (audio->enabled) | |
228 | return 0; | |
229 | ||
230 | /* refuse to start if we're not ready */ | |
231 | if (!audio->out[0].used || !audio->out[1].used) | |
232 | return -EIO; | |
233 | ||
234 | /* we start buffers 0 and 1, so buffer 0 will be the | |
235 | * next one the dsp will want | |
236 | */ | |
237 | audio->out_tail = 0; | |
238 | audio->out_needed = 0; | |
239 | ||
240 | cfg.tx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE; | |
241 | cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_48000; | |
242 | cfg.def_method = RPC_AUD_DEF_METHOD_HOST_PCM; | |
243 | cfg.codec = RPC_AUD_DEF_CODEC_PCM; | |
244 | cfg.snd_method = RPC_SND_METHOD_MIDI; | |
245 | ||
246 | audio_prevent_sleep(audio); | |
247 | rc = audmgr_enable(&audio->audmgr, &cfg); | |
248 | if (rc < 0) { | |
249 | audio_allow_sleep(audio); | |
250 | return rc; | |
251 | } | |
252 | ||
253 | if (audpp_enable(-1, audio_dsp_event, audio)) { | |
254 | pr_err("audio: audpp_enable() failed\n"); | |
255 | audmgr_disable(&audio->audmgr); | |
256 | audio_allow_sleep(audio); | |
257 | return -ENODEV; | |
258 | } | |
259 | ||
260 | audio->enabled = 1; | |
caff4cae IM |
261 | return 0; |
262 | } | |
263 | ||
264 | /* must be called with audio->lock held */ | |
265 | static int audio_disable(struct audio *audio) | |
266 | { | |
267 | pr_info("audio_disable()\n"); | |
268 | if (audio->enabled) { | |
269 | audio->enabled = 0; | |
270 | audio_dsp_out_enable(audio, 0); | |
271 | ||
272 | audpp_disable(-1, audio); | |
273 | ||
274 | wake_up(&audio->wait); | |
275 | audmgr_disable(&audio->audmgr); | |
276 | audio->out_needed = 0; | |
277 | audio_allow_sleep(audio); | |
278 | } | |
279 | return 0; | |
280 | } | |
281 | ||
282 | /* ------------------- dsp --------------------- */ | |
283 | static void audio_dsp_event(void *private, unsigned id, uint16_t *msg) | |
284 | { | |
285 | struct audio *audio = private; | |
286 | struct buffer *frame; | |
287 | unsigned long flags; | |
288 | ||
289 | LOG(EV_DSP_EVENT, id); | |
290 | switch (id) { | |
291 | case AUDPP_MSG_HOST_PCM_INTF_MSG: { | |
292 | unsigned id = msg[2]; | |
293 | unsigned idx = msg[3] - 1; | |
294 | ||
295 | /* pr_info("audio_dsp_event: HOST_PCM id %d idx %d\n", id, idx); */ | |
296 | if (id != AUDPP_MSG_HOSTPCM_ID_ARM_RX) { | |
297 | pr_err("bogus id\n"); | |
298 | break; | |
299 | } | |
300 | if (idx > 1) { | |
301 | pr_err("bogus buffer idx\n"); | |
302 | break; | |
303 | } | |
304 | ||
305 | spin_lock_irqsave(&audio->dsp_lock, flags); | |
306 | if (audio->running) { | |
307 | atomic_add(audio->out[idx].used, &audio->out_bytes); | |
308 | audio->out[idx].used = 0; | |
309 | ||
310 | frame = audio->out + audio->out_tail; | |
311 | if (frame->used) { | |
312 | audio_dsp_send_buffer( | |
313 | audio, audio->out_tail, frame->used); | |
314 | audio->out_tail ^= 1; | |
315 | } else { | |
316 | audio->out_needed++; | |
317 | } | |
318 | wake_up(&audio->wait); | |
319 | } | |
320 | spin_unlock_irqrestore(&audio->dsp_lock, flags); | |
321 | break; | |
322 | } | |
323 | case AUDPP_MSG_PCMDMAMISSED: | |
324 | pr_info("audio_dsp_event: PCMDMAMISSED %d\n", msg[0]); | |
325 | break; | |
326 | case AUDPP_MSG_CFG_MSG: | |
327 | if (msg[0] == AUDPP_MSG_ENA_ENA) { | |
328 | LOG(EV_ENABLE, 1); | |
329 | pr_info("audio_dsp_event: CFG_MSG ENABLE\n"); | |
330 | audio->out_needed = 0; | |
331 | audio->running = 1; | |
332 | audpp_set_volume_and_pan(5, audio->volume, 0); | |
333 | audio_dsp_set_adrc(audio); | |
334 | audio_dsp_set_eq(audio); | |
335 | audio_dsp_set_rx_iir(audio); | |
336 | audio_dsp_out_enable(audio, 1); | |
337 | } else if (msg[0] == AUDPP_MSG_ENA_DIS) { | |
338 | LOG(EV_ENABLE, 0); | |
339 | pr_info("audio_dsp_event: CFG_MSG DISABLE\n"); | |
340 | audio->running = 0; | |
341 | } else { | |
342 | pr_err("audio_dsp_event: CFG_MSG %d?\n", msg[0]); | |
343 | } | |
344 | break; | |
345 | default: | |
346 | pr_err("audio_dsp_event: UNKNOWN (%d)\n", id); | |
347 | } | |
348 | } | |
349 | ||
350 | static int audio_dsp_out_enable(struct audio *audio, int yes) | |
351 | { | |
352 | audpp_cmd_pcm_intf cmd; | |
353 | ||
354 | memset(&cmd, 0, sizeof(cmd)); | |
355 | cmd.cmd_id = AUDPP_CMD_PCM_INTF_2; | |
356 | cmd.object_num = AUDPP_CMD_PCM_INTF_OBJECT_NUM; | |
357 | cmd.config = AUDPP_CMD_PCM_INTF_CONFIG_CMD_V; | |
358 | cmd.intf_type = AUDPP_CMD_PCM_INTF_RX_ENA_ARMTODSP_V; | |
359 | ||
360 | if (yes) { | |
361 | cmd.write_buf1LSW = audio->out[0].addr; | |
362 | cmd.write_buf1MSW = audio->out[0].addr >> 16; | |
363 | cmd.write_buf1_len = audio->out[0].size; | |
364 | cmd.write_buf2LSW = audio->out[1].addr; | |
365 | cmd.write_buf2MSW = audio->out[1].addr >> 16; | |
366 | cmd.write_buf2_len = audio->out[1].size; | |
367 | cmd.arm_to_rx_flag = AUDPP_CMD_PCM_INTF_ENA_V; | |
368 | cmd.weight_decoder_to_rx = audio->out_weight; | |
369 | cmd.weight_arm_to_rx = 1; | |
370 | cmd.partition_number_arm_to_dsp = 0; | |
371 | cmd.sample_rate = audio->out_sample_rate; | |
372 | cmd.channel_mode = audio->out_channel_mode; | |
373 | } | |
374 | ||
375 | return audpp_send_queue2(&cmd, sizeof(cmd)); | |
376 | } | |
377 | ||
378 | static int audio_dsp_send_buffer(struct audio *audio, unsigned idx, unsigned len) | |
379 | { | |
380 | audpp_cmd_pcm_intf_send_buffer cmd; | |
381 | ||
382 | cmd.cmd_id = AUDPP_CMD_PCM_INTF_2; | |
383 | cmd.host_pcm_object = AUDPP_CMD_PCM_INTF_OBJECT_NUM; | |
384 | cmd.config = AUDPP_CMD_PCM_INTF_BUFFER_CMD_V; | |
385 | cmd.intf_type = AUDPP_CMD_PCM_INTF_RX_ENA_ARMTODSP_V; | |
386 | cmd.dsp_to_arm_buf_id = 0; | |
387 | cmd.arm_to_dsp_buf_id = idx + 1; | |
388 | cmd.arm_to_dsp_buf_len = len; | |
389 | ||
390 | LOG(EV_SEND_BUFFER, idx); | |
391 | return audpp_send_queue2(&cmd, sizeof(cmd)); | |
392 | } | |
393 | ||
394 | static int audio_dsp_set_adrc(struct audio *audio) | |
395 | { | |
396 | audpp_cmd_cfg_object_params_adrc cmd; | |
397 | ||
398 | memset(&cmd, 0, sizeof(cmd)); | |
399 | cmd.common.comman_cfg = AUDPP_CMD_CFG_OBJ_UPDATE; | |
400 | cmd.common.command_type = AUDPP_CMD_ADRC; | |
401 | ||
402 | if (audio->adrc_enable) { | |
403 | cmd.adrc_flag = AUDPP_CMD_ADRC_FLAG_ENA; | |
404 | cmd.compression_th = audio->adrc.compression_th; | |
405 | cmd.compression_slope = audio->adrc.compression_slope; | |
406 | cmd.rms_time = audio->adrc.rms_time; | |
407 | cmd.attack_const_lsw = audio->adrc.attack_const_lsw; | |
408 | cmd.attack_const_msw = audio->adrc.attack_const_msw; | |
409 | cmd.release_const_lsw = audio->adrc.release_const_lsw; | |
410 | cmd.release_const_msw = audio->adrc.release_const_msw; | |
411 | cmd.adrc_system_delay = audio->adrc.adrc_system_delay; | |
412 | } else { | |
413 | cmd.adrc_flag = AUDPP_CMD_ADRC_FLAG_DIS; | |
414 | } | |
415 | return audpp_send_queue3(&cmd, sizeof(cmd)); | |
416 | } | |
417 | ||
418 | static int audio_dsp_set_eq(struct audio *audio) | |
419 | { | |
420 | audpp_cmd_cfg_object_params_eq cmd; | |
421 | ||
422 | memset(&cmd, 0, sizeof(cmd)); | |
423 | cmd.common.comman_cfg = AUDPP_CMD_CFG_OBJ_UPDATE; | |
424 | cmd.common.command_type = AUDPP_CMD_EQUALIZER; | |
425 | ||
426 | if (audio->eq_enable) { | |
427 | cmd.eq_flag = AUDPP_CMD_EQ_FLAG_ENA; | |
428 | cmd.num_bands = audio->eq.num_bands; | |
429 | memcpy(&cmd.eq_params, audio->eq.eq_params, | |
430 | sizeof(audio->eq.eq_params)); | |
431 | } else { | |
432 | cmd.eq_flag = AUDPP_CMD_EQ_FLAG_DIS; | |
433 | } | |
434 | return audpp_send_queue3(&cmd, sizeof(cmd)); | |
435 | } | |
436 | ||
437 | static int audio_dsp_set_rx_iir(struct audio *audio) | |
438 | { | |
439 | audpp_cmd_cfg_object_params_rx_iir cmd; | |
440 | ||
441 | memset(&cmd, 0, sizeof(cmd)); | |
442 | cmd.common.comman_cfg = AUDPP_CMD_CFG_OBJ_UPDATE; | |
443 | cmd.common.command_type = AUDPP_CMD_IIR_TUNING_FILTER; | |
444 | ||
445 | if (audio->rx_iir_enable) { | |
446 | cmd.active_flag = AUDPP_CMD_IIR_FLAG_ENA; | |
447 | cmd.num_bands = audio->iir.num_bands; | |
448 | memcpy(&cmd.iir_params, audio->iir.iir_params, | |
449 | sizeof(audio->iir.iir_params)); | |
450 | } else { | |
451 | cmd.active_flag = AUDPP_CMD_IIR_FLAG_DIS; | |
452 | } | |
453 | ||
454 | return audpp_send_queue3(&cmd, sizeof(cmd)); | |
455 | } | |
456 | ||
457 | /* ------------------- device --------------------- */ | |
458 | ||
459 | static int audio_enable_adrc(struct audio *audio, int enable) | |
460 | { | |
461 | if (audio->adrc_enable != enable) { | |
462 | audio->adrc_enable = enable; | |
463 | if (audio->running) | |
464 | audio_dsp_set_adrc(audio); | |
465 | } | |
466 | return 0; | |
467 | } | |
468 | ||
469 | static int audio_enable_eq(struct audio *audio, int enable) | |
470 | { | |
471 | if (audio->eq_enable != enable) { | |
472 | audio->eq_enable = enable; | |
473 | if (audio->running) | |
474 | audio_dsp_set_eq(audio); | |
475 | } | |
476 | return 0; | |
477 | } | |
478 | ||
479 | static int audio_enable_rx_iir(struct audio *audio, int enable) | |
480 | { | |
481 | if (audio->rx_iir_enable != enable) { | |
482 | audio->rx_iir_enable = enable; | |
483 | if (audio->running) | |
484 | audio_dsp_set_rx_iir(audio); | |
485 | } | |
486 | return 0; | |
487 | } | |
488 | ||
489 | static void audio_flush(struct audio *audio) | |
490 | { | |
491 | audio->out[0].used = 0; | |
492 | audio->out[1].used = 0; | |
493 | audio->out_head = 0; | |
494 | audio->out_tail = 0; | |
495 | audio->stopped = 0; | |
496 | } | |
497 | ||
498 | static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
499 | { | |
500 | struct audio *audio = file->private_data; | |
501 | int rc; | |
502 | ||
503 | if (cmd == AUDIO_GET_STATS) { | |
504 | struct msm_audio_stats stats; | |
505 | stats.byte_count = atomic_read(&audio->out_bytes); | |
506 | if (copy_to_user((void*) arg, &stats, sizeof(stats))) | |
507 | return -EFAULT; | |
508 | return 0; | |
509 | } | |
510 | if (cmd == AUDIO_SET_VOLUME) { | |
511 | unsigned long flags; | |
512 | spin_lock_irqsave(&audio->dsp_lock, flags); | |
513 | audio->volume = arg; | |
514 | if (audio->running) | |
515 | audpp_set_volume_and_pan(6, arg, 0); | |
516 | spin_unlock_irqrestore(&audio->dsp_lock, flags); | |
517 | } | |
518 | ||
519 | LOG(EV_IOCTL, cmd); | |
520 | mutex_lock(&audio->lock); | |
521 | switch (cmd) { | |
522 | case AUDIO_START: | |
523 | rc = audio_enable(audio); | |
524 | break; | |
525 | case AUDIO_STOP: | |
526 | rc = audio_disable(audio); | |
527 | audio->stopped = 1; | |
528 | break; | |
529 | case AUDIO_FLUSH: | |
530 | if (audio->stopped) { | |
531 | /* Make sure we're stopped and we wake any threads | |
532 | * that might be blocked holding the write_lock. | |
533 | * While audio->stopped write threads will always | |
534 | * exit immediately. | |
535 | */ | |
536 | wake_up(&audio->wait); | |
537 | mutex_lock(&audio->write_lock); | |
538 | audio_flush(audio); | |
539 | mutex_unlock(&audio->write_lock); | |
540 | } | |
541 | case AUDIO_SET_CONFIG: { | |
542 | struct msm_audio_config config; | |
543 | if (copy_from_user(&config, (void*) arg, sizeof(config))) { | |
544 | rc = -EFAULT; | |
545 | break; | |
546 | } | |
547 | if (config.channel_count == 1) { | |
548 | config.channel_count = AUDPP_CMD_PCM_INTF_MONO_V; | |
549 | } else if (config.channel_count == 2) { | |
550 | config.channel_count= AUDPP_CMD_PCM_INTF_STEREO_V; | |
551 | } else { | |
552 | rc = -EINVAL; | |
553 | break; | |
554 | } | |
555 | audio->out_sample_rate = config.sample_rate; | |
556 | audio->out_channel_mode = config.channel_count; | |
557 | rc = 0; | |
558 | break; | |
559 | } | |
560 | case AUDIO_GET_CONFIG: { | |
561 | struct msm_audio_config config; | |
562 | config.buffer_size = BUFSZ; | |
563 | config.buffer_count = 2; | |
564 | config.sample_rate = audio->out_sample_rate; | |
565 | if (audio->out_channel_mode == AUDPP_CMD_PCM_INTF_MONO_V) { | |
566 | config.channel_count = 1; | |
567 | } else { | |
568 | config.channel_count = 2; | |
569 | } | |
570 | config.unused[0] = 0; | |
571 | config.unused[1] = 0; | |
572 | config.unused[2] = 0; | |
573 | config.unused[3] = 0; | |
574 | if (copy_to_user((void*) arg, &config, sizeof(config))) { | |
575 | rc = -EFAULT; | |
576 | } else { | |
577 | rc = 0; | |
578 | } | |
579 | break; | |
580 | } | |
581 | default: | |
582 | rc = -EINVAL; | |
583 | } | |
584 | mutex_unlock(&audio->lock); | |
585 | return rc; | |
586 | } | |
587 | ||
588 | static ssize_t audio_read(struct file *file, char __user *buf, size_t count, loff_t *pos) | |
589 | { | |
590 | return -EINVAL; | |
591 | } | |
592 | ||
593 | static inline int rt_policy(int policy) | |
594 | { | |
595 | if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR)) | |
596 | return 1; | |
597 | return 0; | |
598 | } | |
599 | ||
600 | static inline int task_has_rt_policy(struct task_struct *p) | |
601 | { | |
602 | return rt_policy(p->policy); | |
603 | } | |
604 | ||
605 | static ssize_t audio_write(struct file *file, const char __user *buf, | |
606 | size_t count, loff_t *pos) | |
607 | { | |
608 | struct sched_param s = { .sched_priority = 1 }; | |
609 | struct audio *audio = file->private_data; | |
610 | unsigned long flags; | |
611 | const char __user *start = buf; | |
612 | struct buffer *frame; | |
613 | size_t xfer; | |
614 | int old_prio = current->rt_priority; | |
615 | int old_policy = current->policy; | |
616 | int cap_nice = cap_raised(current_cap(), CAP_SYS_NICE); | |
617 | int rc = 0; | |
618 | ||
619 | LOG(EV_WRITE, count | (audio->running << 28) | (audio->stopped << 24)); | |
620 | ||
621 | /* just for this write, set us real-time */ | |
622 | if (!task_has_rt_policy(current)) { | |
623 | struct cred *new = prepare_creds(); | |
624 | cap_raise(new->cap_effective, CAP_SYS_NICE); | |
625 | commit_creds(new); | |
626 | sched_setscheduler(current, SCHED_RR, &s); | |
627 | } | |
628 | ||
629 | mutex_lock(&audio->write_lock); | |
630 | while (count > 0) { | |
631 | frame = audio->out + audio->out_head; | |
632 | ||
633 | LOG(EV_WAIT_EVENT, 0); | |
634 | rc = wait_event_interruptible(audio->wait, | |
635 | (frame->used == 0) || (audio->stopped)); | |
636 | LOG(EV_WAIT_EVENT, 1); | |
637 | ||
638 | if (rc < 0) | |
639 | break; | |
640 | if (audio->stopped) { | |
641 | rc = -EBUSY; | |
642 | break; | |
643 | } | |
644 | xfer = count > frame->size ? frame->size : count; | |
645 | if (copy_from_user(frame->data, buf, xfer)) { | |
646 | rc = -EFAULT; | |
647 | break; | |
648 | } | |
649 | frame->used = xfer; | |
650 | audio->out_head ^= 1; | |
651 | count -= xfer; | |
652 | buf += xfer; | |
653 | ||
654 | spin_lock_irqsave(&audio->dsp_lock, flags); | |
655 | LOG(EV_FILL_BUFFER, audio->out_head ^ 1); | |
656 | frame = audio->out + audio->out_tail; | |
657 | if (frame->used && audio->out_needed) { | |
658 | audio_dsp_send_buffer(audio, audio->out_tail, frame->used); | |
659 | audio->out_tail ^= 1; | |
660 | audio->out_needed--; | |
661 | } | |
662 | spin_unlock_irqrestore(&audio->dsp_lock, flags); | |
663 | } | |
664 | ||
665 | mutex_unlock(&audio->write_lock); | |
666 | ||
667 | /* restore scheduling policy and priority */ | |
668 | if (!rt_policy(old_policy)) { | |
669 | struct sched_param v = { .sched_priority = old_prio }; | |
670 | sched_setscheduler(current, old_policy, &v); | |
671 | if (likely(!cap_nice)) { | |
672 | struct cred *new = prepare_creds(); | |
673 | cap_lower(new->cap_effective, CAP_SYS_NICE); | |
674 | commit_creds(new); | |
675 | sched_setscheduler(current, SCHED_RR, &s); | |
676 | } | |
677 | } | |
678 | ||
679 | LOG(EV_RETURN,(buf > start) ? (buf - start) : rc); | |
680 | if (buf > start) | |
681 | return buf - start; | |
682 | return rc; | |
683 | } | |
684 | ||
685 | static int audio_release(struct inode *inode, struct file *file) | |
686 | { | |
687 | struct audio *audio = file->private_data; | |
688 | ||
689 | LOG(EV_OPEN, 0); | |
690 | mutex_lock(&audio->lock); | |
691 | audio_disable(audio); | |
692 | audio_flush(audio); | |
693 | audio->opened = 0; | |
694 | mutex_unlock(&audio->lock); | |
caff4cae IM |
695 | return 0; |
696 | } | |
697 | ||
a5ca2dfc | 698 | static struct audio the_audio; |
caff4cae IM |
699 | |
700 | static int audio_open(struct inode *inode, struct file *file) | |
701 | { | |
702 | struct audio *audio = &the_audio; | |
703 | int rc; | |
704 | ||
705 | mutex_lock(&audio->lock); | |
706 | ||
707 | if (audio->opened) { | |
708 | pr_err("audio: busy\n"); | |
709 | rc = -EBUSY; | |
710 | goto done; | |
711 | } | |
712 | ||
713 | if (!audio->data) { | |
714 | audio->data = dma_alloc_coherent(NULL, DMASZ, | |
715 | &audio->phys, GFP_KERNEL); | |
716 | if (!audio->data) { | |
717 | pr_err("audio: could not allocate DMA buffers\n"); | |
718 | rc = -ENOMEM; | |
719 | goto done; | |
720 | } | |
721 | } | |
722 | ||
723 | rc = audmgr_open(&audio->audmgr); | |
724 | if (rc) | |
725 | goto done; | |
726 | ||
727 | audio->out_buffer_size = BUFSZ; | |
728 | audio->out_sample_rate = 44100; | |
729 | audio->out_channel_mode = AUDPP_CMD_PCM_INTF_STEREO_V; | |
730 | audio->out_weight = 100; | |
731 | ||
732 | audio->out[0].data = audio->data + 0; | |
733 | audio->out[0].addr = audio->phys + 0; | |
734 | audio->out[0].size = BUFSZ; | |
735 | ||
736 | audio->out[1].data = audio->data + BUFSZ; | |
737 | audio->out[1].addr = audio->phys + BUFSZ; | |
738 | audio->out[1].size = BUFSZ; | |
739 | ||
740 | audio->volume = 0x2000; | |
741 | ||
742 | audio_flush(audio); | |
743 | ||
744 | file->private_data = audio; | |
745 | audio->opened = 1; | |
746 | rc = 0; | |
747 | LOG(EV_OPEN, 1); | |
748 | done: | |
749 | mutex_unlock(&audio->lock); | |
750 | return rc; | |
751 | } | |
752 | ||
753 | static long audpp_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
754 | { | |
755 | struct audio *audio = file->private_data; | |
756 | int rc = 0, enable; | |
757 | uint16_t enable_mask; | |
758 | ||
759 | mutex_lock(&audio->lock); | |
760 | switch (cmd) { | |
761 | case AUDIO_ENABLE_AUDPP: | |
762 | if (copy_from_user(&enable_mask, (void *) arg, sizeof(enable_mask))) | |
763 | goto out_fault; | |
764 | ||
765 | enable = (enable_mask & ADRC_ENABLE)? 1 : 0; | |
766 | audio_enable_adrc(audio, enable); | |
767 | enable = (enable_mask & EQ_ENABLE)? 1 : 0; | |
768 | audio_enable_eq(audio, enable); | |
769 | enable = (enable_mask & IIR_ENABLE)? 1 : 0; | |
770 | audio_enable_rx_iir(audio, enable); | |
771 | break; | |
772 | ||
773 | case AUDIO_SET_ADRC: | |
774 | if (copy_from_user(&audio->adrc, (void*) arg, sizeof(audio->adrc))) | |
775 | goto out_fault; | |
776 | break; | |
777 | ||
778 | case AUDIO_SET_EQ: | |
779 | if (copy_from_user(&audio->eq, (void*) arg, sizeof(audio->eq))) | |
780 | goto out_fault; | |
781 | break; | |
782 | ||
783 | case AUDIO_SET_RX_IIR: | |
784 | if (copy_from_user(&audio->iir, (void*) arg, sizeof(audio->iir))) | |
785 | goto out_fault; | |
786 | break; | |
787 | ||
788 | default: | |
789 | rc = -EINVAL; | |
790 | } | |
791 | ||
792 | goto out; | |
793 | ||
794 | out_fault: | |
795 | rc = -EFAULT; | |
796 | out: | |
797 | mutex_unlock(&audio->lock); | |
798 | return rc; | |
799 | } | |
800 | ||
801 | static int audpp_open(struct inode *inode, struct file *file) | |
802 | { | |
803 | struct audio *audio = &the_audio; | |
804 | ||
805 | file->private_data = audio; | |
806 | return 0; | |
807 | } | |
808 | ||
809 | static struct file_operations audio_fops = { | |
810 | .owner = THIS_MODULE, | |
811 | .open = audio_open, | |
812 | .release = audio_release, | |
813 | .read = audio_read, | |
814 | .write = audio_write, | |
815 | .unlocked_ioctl = audio_ioctl, | |
816 | }; | |
817 | ||
818 | static struct file_operations audpp_fops = { | |
819 | .owner = THIS_MODULE, | |
820 | .open = audpp_open, | |
821 | .unlocked_ioctl = audpp_ioctl, | |
822 | }; | |
823 | ||
824 | struct miscdevice audio_misc = { | |
825 | .minor = MISC_DYNAMIC_MINOR, | |
826 | .name = "msm_pcm_out", | |
827 | .fops = &audio_fops, | |
828 | }; | |
829 | ||
830 | struct miscdevice audpp_misc = { | |
831 | .minor = MISC_DYNAMIC_MINOR, | |
832 | .name = "msm_pcm_ctl", | |
833 | .fops = &audpp_fops, | |
834 | }; | |
835 | ||
836 | static int __init audio_init(void) | |
837 | { | |
838 | mutex_init(&the_audio.lock); | |
839 | mutex_init(&the_audio.write_lock); | |
840 | spin_lock_init(&the_audio.dsp_lock); | |
841 | init_waitqueue_head(&the_audio.wait); | |
842 | wake_lock_init(&the_audio.wakelock, WAKE_LOCK_SUSPEND, "audio_pcm"); | |
843 | wake_lock_init(&the_audio.idlelock, WAKE_LOCK_IDLE, "audio_pcm_idle"); | |
844 | return (misc_register(&audio_misc) || misc_register(&audpp_misc)); | |
845 | } | |
846 | ||
847 | device_initcall(audio_init); |