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