]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (c) by Jaroslav Kysela <perex@suse.cz> | |
3 | * Creative Labs, Inc. | |
4 | * Routines for control of EMU10K1 chips / PCM routines | |
5 | * Multichannel PCM support Copyright (c) Lee Revell <rlrevell@joe-job.com> | |
6 | * | |
7 | * BUGS: | |
8 | * -- | |
9 | * | |
10 | * TODO: | |
11 | * -- | |
12 | * | |
13 | * This program is free software; you can redistribute it and/or modify | |
14 | * it under the terms of the GNU General Public License as published by | |
15 | * the Free Software Foundation; either version 2 of the License, or | |
16 | * (at your option) any later version. | |
17 | * | |
18 | * This program is distributed in the hope that it will be useful, | |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | * GNU General Public License for more details. | |
22 | * | |
23 | * You should have received a copy of the GNU General Public License | |
24 | * along with this program; if not, write to the Free Software | |
25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
26 | * | |
27 | */ | |
28 | ||
29 | #include <sound/driver.h> | |
30 | #include <linux/pci.h> | |
31 | #include <linux/delay.h> | |
32 | #include <linux/slab.h> | |
33 | #include <linux/time.h> | |
34 | #include <linux/init.h> | |
35 | #include <sound/core.h> | |
36 | #include <sound/emu10k1.h> | |
37 | ||
38 | static void snd_emu10k1_pcm_interrupt(emu10k1_t *emu, emu10k1_voice_t *voice) | |
39 | { | |
40 | emu10k1_pcm_t *epcm; | |
41 | ||
42 | if ((epcm = voice->epcm) == NULL) | |
43 | return; | |
44 | if (epcm->substream == NULL) | |
45 | return; | |
46 | #if 0 | |
47 | printk("IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n", | |
48 | epcm->substream->runtime->hw->pointer(emu, epcm->substream), | |
49 | snd_pcm_lib_period_bytes(epcm->substream), | |
50 | snd_pcm_lib_buffer_bytes(epcm->substream)); | |
51 | #endif | |
52 | snd_pcm_period_elapsed(epcm->substream); | |
53 | } | |
54 | ||
55 | static void snd_emu10k1_pcm_ac97adc_interrupt(emu10k1_t *emu, unsigned int status) | |
56 | { | |
57 | #if 0 | |
58 | if (status & IPR_ADCBUFHALFFULL) { | |
59 | if (emu->pcm_capture_substream->runtime->mode == SNDRV_PCM_MODE_FRAME) | |
60 | return; | |
61 | } | |
62 | #endif | |
63 | snd_pcm_period_elapsed(emu->pcm_capture_substream); | |
64 | } | |
65 | ||
66 | static void snd_emu10k1_pcm_ac97mic_interrupt(emu10k1_t *emu, unsigned int status) | |
67 | { | |
68 | #if 0 | |
69 | if (status & IPR_MICBUFHALFFULL) { | |
70 | if (emu->pcm_capture_mic_substream->runtime->mode == SNDRV_PCM_MODE_FRAME) | |
71 | return; | |
72 | } | |
73 | #endif | |
74 | snd_pcm_period_elapsed(emu->pcm_capture_mic_substream); | |
75 | } | |
76 | ||
77 | static void snd_emu10k1_pcm_efx_interrupt(emu10k1_t *emu, unsigned int status) | |
78 | { | |
79 | #if 0 | |
80 | if (status & IPR_EFXBUFHALFFULL) { | |
81 | if (emu->pcm_capture_efx_substream->runtime->mode == SNDRV_PCM_MODE_FRAME) | |
82 | return; | |
83 | } | |
84 | #endif | |
85 | snd_pcm_period_elapsed(emu->pcm_capture_efx_substream); | |
86 | } | |
87 | ||
88 | static snd_pcm_uframes_t snd_emu10k1_efx_playback_pointer(snd_pcm_substream_t * substream) | |
89 | { | |
90 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
91 | snd_pcm_runtime_t *runtime = substream->runtime; | |
92 | emu10k1_pcm_t *epcm = runtime->private_data; | |
93 | unsigned int ptr; | |
94 | ||
95 | if (!epcm->running) | |
96 | return 0; | |
97 | ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff; | |
98 | ptr += runtime->buffer_size; | |
99 | ptr -= epcm->ccca_start_addr; | |
100 | ptr %= runtime->buffer_size; | |
101 | ||
102 | return ptr; | |
103 | } | |
104 | ||
105 | static int snd_emu10k1_pcm_channel_alloc(emu10k1_pcm_t * epcm, int voices) | |
106 | { | |
107 | int err, i; | |
108 | ||
109 | if (epcm->voices[1] != NULL && voices < 2) { | |
110 | snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]); | |
111 | epcm->voices[1] = NULL; | |
112 | } | |
113 | for (i = 0; i < voices; i++) { | |
114 | if (epcm->voices[i] == NULL) | |
115 | break; | |
116 | } | |
117 | if (i == voices) | |
118 | return 0; /* already allocated */ | |
119 | ||
120 | for (i = 0; i < ARRAY_SIZE(epcm->voices); i++) { | |
121 | if (epcm->voices[i]) { | |
122 | snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]); | |
123 | epcm->voices[i] = NULL; | |
124 | } | |
125 | } | |
126 | err = snd_emu10k1_voice_alloc(epcm->emu, | |
127 | epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX, | |
128 | voices, | |
129 | &epcm->voices[0]); | |
130 | ||
131 | if (err < 0) | |
132 | return err; | |
133 | epcm->voices[0]->epcm = epcm; | |
134 | if (voices > 1) { | |
135 | for (i = 1; i < voices; i++) { | |
136 | epcm->voices[i] = &epcm->emu->voices[epcm->voices[0]->number + i]; | |
137 | epcm->voices[i]->epcm = epcm; | |
138 | } | |
139 | } | |
140 | if (epcm->extra == NULL) { | |
141 | err = snd_emu10k1_voice_alloc(epcm->emu, | |
142 | epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX, | |
143 | 1, | |
144 | &epcm->extra); | |
145 | if (err < 0) { | |
146 | // printk("pcm_channel_alloc: failed extra: voices=%d, frame=%d\n", voices, frame); | |
147 | for (i = 0; i < voices; i++) { | |
148 | snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]); | |
149 | epcm->voices[i] = NULL; | |
150 | } | |
151 | return err; | |
152 | } | |
153 | epcm->extra->epcm = epcm; | |
154 | epcm->extra->interrupt = snd_emu10k1_pcm_interrupt; | |
155 | } | |
156 | return 0; | |
157 | } | |
158 | ||
159 | static unsigned int capture_period_sizes[31] = { | |
160 | 384, 448, 512, 640, | |
161 | 384*2, 448*2, 512*2, 640*2, | |
162 | 384*4, 448*4, 512*4, 640*4, | |
163 | 384*8, 448*8, 512*8, 640*8, | |
164 | 384*16, 448*16, 512*16, 640*16, | |
165 | 384*32, 448*32, 512*32, 640*32, | |
166 | 384*64, 448*64, 512*64, 640*64, | |
167 | 384*128,448*128,512*128 | |
168 | }; | |
169 | ||
170 | static snd_pcm_hw_constraint_list_t hw_constraints_capture_period_sizes = { | |
171 | .count = 31, | |
172 | .list = capture_period_sizes, | |
173 | .mask = 0 | |
174 | }; | |
175 | ||
176 | static unsigned int capture_rates[8] = { | |
177 | 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000 | |
178 | }; | |
179 | ||
180 | static snd_pcm_hw_constraint_list_t hw_constraints_capture_rates = { | |
181 | .count = 8, | |
182 | .list = capture_rates, | |
183 | .mask = 0 | |
184 | }; | |
185 | ||
186 | static unsigned int snd_emu10k1_capture_rate_reg(unsigned int rate) | |
187 | { | |
188 | switch (rate) { | |
189 | case 8000: return ADCCR_SAMPLERATE_8; | |
190 | case 11025: return ADCCR_SAMPLERATE_11; | |
191 | case 16000: return ADCCR_SAMPLERATE_16; | |
192 | case 22050: return ADCCR_SAMPLERATE_22; | |
193 | case 24000: return ADCCR_SAMPLERATE_24; | |
194 | case 32000: return ADCCR_SAMPLERATE_32; | |
195 | case 44100: return ADCCR_SAMPLERATE_44; | |
196 | case 48000: return ADCCR_SAMPLERATE_48; | |
197 | default: | |
198 | snd_BUG(); | |
199 | return ADCCR_SAMPLERATE_8; | |
200 | } | |
201 | } | |
202 | ||
203 | static unsigned int snd_emu10k1_audigy_capture_rate_reg(unsigned int rate) | |
204 | { | |
205 | switch (rate) { | |
206 | case 8000: return A_ADCCR_SAMPLERATE_8; | |
207 | case 11025: return A_ADCCR_SAMPLERATE_11; | |
208 | case 12000: return A_ADCCR_SAMPLERATE_12; /* really supported? */ | |
209 | case 16000: return ADCCR_SAMPLERATE_16; | |
210 | case 22050: return ADCCR_SAMPLERATE_22; | |
211 | case 24000: return ADCCR_SAMPLERATE_24; | |
212 | case 32000: return ADCCR_SAMPLERATE_32; | |
213 | case 44100: return ADCCR_SAMPLERATE_44; | |
214 | case 48000: return ADCCR_SAMPLERATE_48; | |
215 | default: | |
216 | snd_BUG(); | |
217 | return A_ADCCR_SAMPLERATE_8; | |
218 | } | |
219 | } | |
220 | ||
221 | static unsigned int emu10k1_calc_pitch_target(unsigned int rate) | |
222 | { | |
223 | unsigned int pitch_target; | |
224 | ||
225 | pitch_target = (rate << 8) / 375; | |
226 | pitch_target = (pitch_target >> 1) + (pitch_target & 1); | |
227 | return pitch_target; | |
228 | } | |
229 | ||
230 | #define PITCH_48000 0x00004000 | |
231 | #define PITCH_96000 0x00008000 | |
232 | #define PITCH_85000 0x00007155 | |
233 | #define PITCH_80726 0x00006ba2 | |
234 | #define PITCH_67882 0x00005a82 | |
235 | #define PITCH_57081 0x00004c1c | |
236 | ||
237 | static unsigned int emu10k1_select_interprom(unsigned int pitch_target) | |
238 | { | |
239 | if (pitch_target == PITCH_48000) | |
240 | return CCCA_INTERPROM_0; | |
241 | else if (pitch_target < PITCH_48000) | |
242 | return CCCA_INTERPROM_1; | |
243 | else if (pitch_target >= PITCH_96000) | |
244 | return CCCA_INTERPROM_0; | |
245 | else if (pitch_target >= PITCH_85000) | |
246 | return CCCA_INTERPROM_6; | |
247 | else if (pitch_target >= PITCH_80726) | |
248 | return CCCA_INTERPROM_5; | |
249 | else if (pitch_target >= PITCH_67882) | |
250 | return CCCA_INTERPROM_4; | |
251 | else if (pitch_target >= PITCH_57081) | |
252 | return CCCA_INTERPROM_3; | |
253 | else | |
254 | return CCCA_INTERPROM_2; | |
255 | } | |
256 | ||
257 | /* | |
258 | * calculate cache invalidate size | |
259 | * | |
260 | * stereo: channel is stereo | |
261 | * w_16: using 16bit samples | |
262 | * | |
263 | * returns: cache invalidate size in samples | |
264 | */ | |
fe5ac9dc | 265 | static inline int emu10k1_ccis(int stereo, int w_16) |
1da177e4 LT |
266 | { |
267 | if (w_16) { | |
268 | return stereo ? 24 : 26; | |
269 | } else { | |
270 | return stereo ? 24*2 : 26*2; | |
271 | } | |
272 | } | |
273 | ||
274 | static void snd_emu10k1_pcm_init_voice(emu10k1_t *emu, | |
275 | int master, int extra, | |
276 | emu10k1_voice_t *evoice, | |
277 | unsigned int start_addr, | |
278 | unsigned int end_addr, | |
279 | emu10k1_pcm_mixer_t *mix) | |
280 | { | |
281 | snd_pcm_substream_t *substream = evoice->epcm->substream; | |
282 | snd_pcm_runtime_t *runtime = substream->runtime; | |
283 | unsigned int silent_page, tmp; | |
284 | int voice, stereo, w_16; | |
285 | unsigned char attn, send_amount[8]; | |
286 | unsigned char send_routing[8]; | |
287 | unsigned long flags; | |
288 | unsigned int pitch_target; | |
289 | unsigned int ccis; | |
290 | ||
291 | voice = evoice->number; | |
292 | stereo = runtime->channels == 2; | |
293 | w_16 = snd_pcm_format_width(runtime->format) == 16; | |
294 | ||
295 | if (!extra && stereo) { | |
296 | start_addr >>= 1; | |
297 | end_addr >>= 1; | |
298 | } | |
299 | if (w_16) { | |
300 | start_addr >>= 1; | |
301 | end_addr >>= 1; | |
302 | } | |
303 | ||
304 | spin_lock_irqsave(&emu->reg_lock, flags); | |
305 | ||
306 | /* volume parameters */ | |
307 | if (extra) { | |
308 | attn = 0; | |
309 | memset(send_routing, 0, sizeof(send_routing)); | |
310 | send_routing[0] = 0; | |
311 | send_routing[1] = 1; | |
312 | send_routing[2] = 2; | |
313 | send_routing[3] = 3; | |
314 | memset(send_amount, 0, sizeof(send_amount)); | |
315 | } else { | |
316 | /* mono, left, right (master voice = left) */ | |
317 | tmp = stereo ? (master ? 1 : 2) : 0; | |
318 | memcpy(send_routing, &mix->send_routing[tmp][0], 8); | |
319 | memcpy(send_amount, &mix->send_volume[tmp][0], 8); | |
320 | } | |
321 | ||
322 | ccis = emu10k1_ccis(stereo, w_16); | |
323 | ||
324 | if (master) { | |
325 | evoice->epcm->ccca_start_addr = start_addr + ccis; | |
326 | if (extra) { | |
327 | start_addr += ccis; | |
328 | end_addr += ccis; | |
329 | } | |
330 | if (stereo && !extra) { | |
331 | snd_emu10k1_ptr_write(emu, CPF, voice, CPF_STEREO_MASK); | |
332 | snd_emu10k1_ptr_write(emu, CPF, (voice + 1), CPF_STEREO_MASK); | |
333 | } else { | |
334 | snd_emu10k1_ptr_write(emu, CPF, voice, 0); | |
335 | } | |
336 | } | |
337 | ||
338 | // setup routing | |
339 | if (emu->audigy) { | |
340 | snd_emu10k1_ptr_write(emu, A_FXRT1, voice, | |
341 | snd_emu10k1_compose_audigy_fxrt1(send_routing)); | |
342 | snd_emu10k1_ptr_write(emu, A_FXRT2, voice, | |
343 | snd_emu10k1_compose_audigy_fxrt2(send_routing)); | |
344 | snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice, | |
345 | ((unsigned int)send_amount[4] << 24) | | |
346 | ((unsigned int)send_amount[5] << 16) | | |
347 | ((unsigned int)send_amount[6] << 8) | | |
348 | (unsigned int)send_amount[7]); | |
349 | } else | |
350 | snd_emu10k1_ptr_write(emu, FXRT, voice, | |
351 | snd_emu10k1_compose_send_routing(send_routing)); | |
352 | // Stop CA | |
353 | // Assumption that PT is already 0 so no harm overwriting | |
354 | snd_emu10k1_ptr_write(emu, PTRX, voice, (send_amount[0] << 8) | send_amount[1]); | |
355 | snd_emu10k1_ptr_write(emu, DSL, voice, end_addr | (send_amount[3] << 24)); | |
356 | snd_emu10k1_ptr_write(emu, PSST, voice, start_addr | (send_amount[2] << 24)); | |
357 | pitch_target = emu10k1_calc_pitch_target(runtime->rate); | |
358 | if (extra) | |
359 | snd_emu10k1_ptr_write(emu, CCCA, voice, start_addr | | |
360 | emu10k1_select_interprom(pitch_target) | | |
361 | (w_16 ? 0 : CCCA_8BITSELECT)); | |
362 | else | |
363 | snd_emu10k1_ptr_write(emu, CCCA, voice, (start_addr + ccis) | | |
364 | emu10k1_select_interprom(pitch_target) | | |
365 | (w_16 ? 0 : CCCA_8BITSELECT)); | |
366 | // Clear filter delay memory | |
367 | snd_emu10k1_ptr_write(emu, Z1, voice, 0); | |
368 | snd_emu10k1_ptr_write(emu, Z2, voice, 0); | |
369 | // invalidate maps | |
370 | silent_page = ((unsigned int)emu->silent_page.addr << 1) | MAP_PTI_MASK; | |
371 | snd_emu10k1_ptr_write(emu, MAPA, voice, silent_page); | |
372 | snd_emu10k1_ptr_write(emu, MAPB, voice, silent_page); | |
373 | // modulation envelope | |
374 | snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff); | |
375 | snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff); | |
376 | snd_emu10k1_ptr_write(emu, ATKHLDM, voice, 0); | |
377 | snd_emu10k1_ptr_write(emu, DCYSUSM, voice, 0x007f); | |
378 | snd_emu10k1_ptr_write(emu, LFOVAL1, voice, 0x8000); | |
379 | snd_emu10k1_ptr_write(emu, LFOVAL2, voice, 0x8000); | |
380 | snd_emu10k1_ptr_write(emu, FMMOD, voice, 0); | |
381 | snd_emu10k1_ptr_write(emu, TREMFRQ, voice, 0); | |
382 | snd_emu10k1_ptr_write(emu, FM2FRQ2, voice, 0); | |
383 | snd_emu10k1_ptr_write(emu, ENVVAL, voice, 0x8000); | |
384 | // volume envelope | |
385 | snd_emu10k1_ptr_write(emu, ATKHLDV, voice, 0x7f7f); | |
386 | snd_emu10k1_ptr_write(emu, ENVVOL, voice, 0x0000); | |
387 | // filter envelope | |
388 | snd_emu10k1_ptr_write(emu, PEFE_FILTERAMOUNT, voice, 0x7f); | |
389 | // pitch envelope | |
390 | snd_emu10k1_ptr_write(emu, PEFE_PITCHAMOUNT, voice, 0); | |
391 | ||
392 | spin_unlock_irqrestore(&emu->reg_lock, flags); | |
393 | } | |
394 | ||
395 | static int snd_emu10k1_playback_hw_params(snd_pcm_substream_t * substream, | |
396 | snd_pcm_hw_params_t * hw_params) | |
397 | { | |
398 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
399 | snd_pcm_runtime_t *runtime = substream->runtime; | |
400 | emu10k1_pcm_t *epcm = runtime->private_data; | |
401 | int err; | |
402 | ||
403 | if ((err = snd_emu10k1_pcm_channel_alloc(epcm, params_channels(hw_params))) < 0) | |
404 | return err; | |
405 | if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0) | |
406 | return err; | |
407 | if (err > 0) { /* change */ | |
e017fa57 | 408 | int mapped; |
1da177e4 LT |
409 | if (epcm->memblk != NULL) |
410 | snd_emu10k1_free_pages(emu, epcm->memblk); | |
e017fa57 TI |
411 | epcm->memblk = snd_emu10k1_alloc_pages(emu, substream); |
412 | epcm->start_addr = 0; | |
413 | if (! epcm->memblk) | |
1da177e4 | 414 | return -ENOMEM; |
e017fa57 TI |
415 | mapped = ((emu10k1_memblk_t *)epcm->memblk)->mapped_page; |
416 | if (mapped < 0) | |
417 | return -ENOMEM; | |
418 | epcm->start_addr = mapped << PAGE_SHIFT; | |
1da177e4 LT |
419 | } |
420 | return 0; | |
421 | } | |
422 | ||
423 | static int snd_emu10k1_playback_hw_free(snd_pcm_substream_t * substream) | |
424 | { | |
425 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
426 | snd_pcm_runtime_t *runtime = substream->runtime; | |
427 | emu10k1_pcm_t *epcm; | |
428 | ||
429 | if (runtime->private_data == NULL) | |
430 | return 0; | |
431 | epcm = runtime->private_data; | |
432 | if (epcm->extra) { | |
433 | snd_emu10k1_voice_free(epcm->emu, epcm->extra); | |
434 | epcm->extra = NULL; | |
435 | } | |
436 | if (epcm->voices[1]) { | |
437 | snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]); | |
438 | epcm->voices[1] = NULL; | |
439 | } | |
440 | if (epcm->voices[0]) { | |
441 | snd_emu10k1_voice_free(epcm->emu, epcm->voices[0]); | |
442 | epcm->voices[0] = NULL; | |
443 | } | |
444 | if (epcm->memblk) { | |
445 | snd_emu10k1_free_pages(emu, epcm->memblk); | |
446 | epcm->memblk = NULL; | |
447 | epcm->start_addr = 0; | |
448 | } | |
449 | snd_pcm_lib_free_pages(substream); | |
450 | return 0; | |
451 | } | |
452 | ||
453 | static int snd_emu10k1_efx_playback_hw_free(snd_pcm_substream_t * substream) | |
454 | { | |
455 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
456 | snd_pcm_runtime_t *runtime = substream->runtime; | |
457 | emu10k1_pcm_t *epcm; | |
458 | int i; | |
459 | ||
460 | if (runtime->private_data == NULL) | |
461 | return 0; | |
462 | epcm = runtime->private_data; | |
463 | if (epcm->extra) { | |
464 | snd_emu10k1_voice_free(epcm->emu, epcm->extra); | |
465 | epcm->extra = NULL; | |
466 | } | |
467 | for (i=0; i < NUM_EFX_PLAYBACK; i++) { | |
468 | if (epcm->voices[i]) { | |
469 | snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]); | |
470 | epcm->voices[i] = NULL; | |
471 | } | |
472 | } | |
473 | if (epcm->memblk) { | |
474 | snd_emu10k1_free_pages(emu, epcm->memblk); | |
475 | epcm->memblk = NULL; | |
476 | epcm->start_addr = 0; | |
477 | } | |
478 | snd_pcm_lib_free_pages(substream); | |
479 | return 0; | |
480 | } | |
481 | ||
482 | static int snd_emu10k1_playback_prepare(snd_pcm_substream_t * substream) | |
483 | { | |
484 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
485 | snd_pcm_runtime_t *runtime = substream->runtime; | |
486 | emu10k1_pcm_t *epcm = runtime->private_data; | |
487 | unsigned int start_addr, end_addr; | |
488 | ||
489 | start_addr = epcm->start_addr; | |
490 | end_addr = snd_pcm_lib_period_bytes(substream); | |
491 | if (runtime->channels == 2) { | |
492 | start_addr >>= 1; | |
493 | end_addr >>= 1; | |
494 | } | |
495 | end_addr += start_addr; | |
496 | snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra, | |
497 | start_addr, end_addr, NULL); | |
498 | start_addr = epcm->start_addr; | |
499 | end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream); | |
500 | snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0], | |
501 | start_addr, end_addr, | |
502 | &emu->pcm_mixer[substream->number]); | |
503 | if (epcm->voices[1]) | |
504 | snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[1], | |
505 | start_addr, end_addr, | |
506 | &emu->pcm_mixer[substream->number]); | |
507 | return 0; | |
508 | } | |
509 | ||
510 | static int snd_emu10k1_efx_playback_prepare(snd_pcm_substream_t * substream) | |
511 | { | |
512 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
513 | snd_pcm_runtime_t *runtime = substream->runtime; | |
514 | emu10k1_pcm_t *epcm = runtime->private_data; | |
515 | unsigned int start_addr, end_addr; | |
516 | unsigned int channel_size; | |
517 | int i; | |
518 | ||
519 | start_addr = epcm->start_addr; | |
520 | end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream); | |
521 | ||
522 | /* | |
523 | * the kX driver leaves some space between voices | |
524 | */ | |
525 | channel_size = ( end_addr - start_addr ) / NUM_EFX_PLAYBACK; | |
526 | ||
527 | snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra, | |
528 | start_addr, start_addr + (channel_size / 2), NULL); | |
529 | ||
530 | /* only difference with the master voice is we use it for the pointer */ | |
531 | snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0], | |
532 | start_addr, start_addr + channel_size, | |
533 | &emu->efx_pcm_mixer[0]); | |
534 | ||
535 | start_addr += channel_size; | |
536 | for (i = 1; i < NUM_EFX_PLAYBACK; i++) { | |
537 | snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[i], | |
538 | start_addr, start_addr + channel_size, | |
539 | &emu->efx_pcm_mixer[i]); | |
540 | start_addr += channel_size; | |
541 | } | |
542 | ||
543 | return 0; | |
544 | } | |
545 | ||
546 | static snd_pcm_hardware_t snd_emu10k1_efx_playback = | |
547 | { | |
548 | .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_NONINTERLEAVED | | |
549 | SNDRV_PCM_INFO_BLOCK_TRANSFER | | |
550 | SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE), | |
551 | .formats = SNDRV_PCM_FMTBIT_S16_LE, | |
552 | .rates = SNDRV_PCM_RATE_48000, | |
553 | .rate_min = 48000, | |
554 | .rate_max = 48000, | |
555 | .channels_min = NUM_EFX_PLAYBACK, | |
556 | .channels_max = NUM_EFX_PLAYBACK, | |
557 | .buffer_bytes_max = (64*1024), | |
558 | .period_bytes_min = 64, | |
559 | .period_bytes_max = (64*1024), | |
560 | .periods_min = 2, | |
561 | .periods_max = 2, | |
562 | .fifo_size = 0, | |
563 | }; | |
564 | ||
565 | static int snd_emu10k1_capture_hw_params(snd_pcm_substream_t * substream, | |
566 | snd_pcm_hw_params_t * hw_params) | |
567 | { | |
568 | return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)); | |
569 | } | |
570 | ||
571 | static int snd_emu10k1_capture_hw_free(snd_pcm_substream_t * substream) | |
572 | { | |
573 | return snd_pcm_lib_free_pages(substream); | |
574 | } | |
575 | ||
576 | static int snd_emu10k1_capture_prepare(snd_pcm_substream_t * substream) | |
577 | { | |
578 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
579 | snd_pcm_runtime_t *runtime = substream->runtime; | |
580 | emu10k1_pcm_t *epcm = runtime->private_data; | |
581 | int idx; | |
582 | ||
583 | /* zeroing the buffer size will stop capture */ | |
584 | snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0); | |
585 | switch (epcm->type) { | |
586 | case CAPTURE_AC97ADC: | |
587 | snd_emu10k1_ptr_write(emu, ADCCR, 0, 0); | |
588 | break; | |
589 | case CAPTURE_EFX: | |
590 | if (emu->audigy) { | |
591 | snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0); | |
592 | snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0); | |
593 | } else | |
594 | snd_emu10k1_ptr_write(emu, FXWC, 0, 0); | |
595 | break; | |
596 | default: | |
597 | break; | |
598 | } | |
599 | snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr); | |
600 | epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream); | |
601 | epcm->capture_bs_val = 0; | |
602 | for (idx = 0; idx < 31; idx++) { | |
603 | if (capture_period_sizes[idx] == epcm->capture_bufsize) { | |
604 | epcm->capture_bs_val = idx + 1; | |
605 | break; | |
606 | } | |
607 | } | |
608 | if (epcm->capture_bs_val == 0) { | |
609 | snd_BUG(); | |
610 | epcm->capture_bs_val++; | |
611 | } | |
612 | if (epcm->type == CAPTURE_AC97ADC) { | |
613 | epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE; | |
614 | if (runtime->channels > 1) | |
615 | epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE; | |
616 | epcm->capture_cr_val |= emu->audigy ? | |
617 | snd_emu10k1_audigy_capture_rate_reg(runtime->rate) : | |
618 | snd_emu10k1_capture_rate_reg(runtime->rate); | |
619 | } | |
620 | return 0; | |
621 | } | |
622 | ||
623 | static void snd_emu10k1_playback_invalidate_cache(emu10k1_t *emu, int extra, emu10k1_voice_t *evoice) | |
624 | { | |
625 | snd_pcm_runtime_t *runtime; | |
626 | unsigned int voice, stereo, i, ccis, cra = 64, cs, sample; | |
627 | ||
628 | if (evoice == NULL) | |
629 | return; | |
630 | runtime = evoice->epcm->substream->runtime; | |
631 | voice = evoice->number; | |
632 | stereo = (!extra && runtime->channels == 2); | |
633 | sample = snd_pcm_format_width(runtime->format) == 16 ? 0 : 0x80808080; | |
634 | ccis = emu10k1_ccis(stereo, sample == 0); | |
635 | // set cs to 2 * number of cache registers beside the invalidated | |
636 | cs = (sample == 0) ? (32-ccis) : (64-ccis+1) >> 1; | |
637 | if (cs > 16) cs = 16; | |
638 | for (i = 0; i < cs; i++) { | |
639 | snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample); | |
640 | if (stereo) { | |
641 | snd_emu10k1_ptr_write(emu, CD0 + i, voice + 1, sample); | |
642 | } | |
643 | } | |
644 | // reset cache | |
645 | snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, 0); | |
646 | snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice, cra); | |
647 | if (stereo) { | |
648 | snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice + 1, 0); | |
649 | snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice + 1, cra); | |
650 | } | |
651 | // fill cache | |
652 | snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, ccis); | |
653 | if (stereo) { | |
654 | snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice+1, ccis); | |
655 | } | |
656 | } | |
657 | ||
658 | static void snd_emu10k1_playback_prepare_voice(emu10k1_t *emu, emu10k1_voice_t *evoice, | |
659 | int master, int extra, | |
660 | emu10k1_pcm_mixer_t *mix) | |
661 | { | |
662 | snd_pcm_substream_t *substream; | |
663 | snd_pcm_runtime_t *runtime; | |
664 | unsigned int attn, vattn; | |
665 | unsigned int voice, tmp; | |
666 | ||
667 | if (evoice == NULL) /* skip second voice for mono */ | |
668 | return; | |
669 | substream = evoice->epcm->substream; | |
670 | runtime = substream->runtime; | |
671 | voice = evoice->number; | |
672 | ||
673 | attn = extra ? 0 : 0x00ff; | |
674 | tmp = runtime->channels == 2 ? (master ? 1 : 2) : 0; | |
675 | vattn = mix != NULL ? (mix->attn[tmp] << 16) : 0; | |
676 | snd_emu10k1_ptr_write(emu, IFATN, voice, attn); | |
677 | snd_emu10k1_ptr_write(emu, VTFT, voice, vattn | 0xffff); | |
678 | snd_emu10k1_ptr_write(emu, CVCF, voice, vattn | 0xffff); | |
679 | snd_emu10k1_ptr_write(emu, DCYSUSV, voice, 0x7f7f); | |
680 | snd_emu10k1_voice_clear_loop_stop(emu, voice); | |
681 | } | |
682 | ||
683 | static void snd_emu10k1_playback_trigger_voice(emu10k1_t *emu, emu10k1_voice_t *evoice, int master, int extra) | |
684 | { | |
685 | snd_pcm_substream_t *substream; | |
686 | snd_pcm_runtime_t *runtime; | |
687 | unsigned int voice, pitch, pitch_target; | |
688 | ||
689 | if (evoice == NULL) /* skip second voice for mono */ | |
690 | return; | |
691 | substream = evoice->epcm->substream; | |
692 | runtime = substream->runtime; | |
693 | voice = evoice->number; | |
694 | ||
695 | pitch = snd_emu10k1_rate_to_pitch(runtime->rate) >> 8; | |
696 | pitch_target = emu10k1_calc_pitch_target(runtime->rate); | |
697 | snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, pitch_target); | |
698 | if (master || evoice->epcm->type == PLAYBACK_EFX) | |
699 | snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, pitch_target); | |
700 | snd_emu10k1_ptr_write(emu, IP, voice, pitch); | |
701 | if (extra) | |
702 | snd_emu10k1_voice_intr_enable(emu, voice); | |
703 | } | |
704 | ||
705 | static void snd_emu10k1_playback_stop_voice(emu10k1_t *emu, emu10k1_voice_t *evoice) | |
706 | { | |
707 | unsigned int voice; | |
708 | ||
709 | if (evoice == NULL) | |
710 | return; | |
711 | voice = evoice->number; | |
712 | snd_emu10k1_voice_intr_disable(emu, voice); | |
713 | snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, 0); | |
714 | snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, 0); | |
715 | snd_emu10k1_ptr_write(emu, IFATN, voice, 0xffff); | |
716 | snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff); | |
717 | snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff); | |
718 | snd_emu10k1_ptr_write(emu, IP, voice, 0); | |
719 | } | |
720 | ||
721 | static int snd_emu10k1_playback_trigger(snd_pcm_substream_t * substream, | |
722 | int cmd) | |
723 | { | |
724 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
725 | snd_pcm_runtime_t *runtime = substream->runtime; | |
726 | emu10k1_pcm_t *epcm = runtime->private_data; | |
727 | emu10k1_pcm_mixer_t *mix; | |
728 | int result = 0; | |
729 | ||
730 | // printk("trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n", (int)emu, cmd, substream->ops->pointer(substream)); | |
731 | spin_lock(&emu->reg_lock); | |
732 | switch (cmd) { | |
733 | case SNDRV_PCM_TRIGGER_START: | |
734 | snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra); /* do we need this? */ | |
735 | snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[0]); | |
736 | /* follow thru */ | |
737 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
738 | mix = &emu->pcm_mixer[substream->number]; | |
739 | snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 1, 0, mix); | |
740 | snd_emu10k1_playback_prepare_voice(emu, epcm->voices[1], 0, 0, mix); | |
741 | snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL); | |
742 | snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 1, 0); | |
743 | snd_emu10k1_playback_trigger_voice(emu, epcm->voices[1], 0, 0); | |
744 | snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1); | |
745 | epcm->running = 1; | |
746 | break; | |
747 | case SNDRV_PCM_TRIGGER_STOP: | |
748 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
749 | epcm->running = 0; | |
750 | snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]); | |
751 | snd_emu10k1_playback_stop_voice(emu, epcm->voices[1]); | |
752 | snd_emu10k1_playback_stop_voice(emu, epcm->extra); | |
753 | break; | |
754 | default: | |
755 | result = -EINVAL; | |
756 | break; | |
757 | } | |
758 | spin_unlock(&emu->reg_lock); | |
759 | return result; | |
760 | } | |
761 | ||
762 | static int snd_emu10k1_capture_trigger(snd_pcm_substream_t * substream, | |
763 | int cmd) | |
764 | { | |
765 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
766 | snd_pcm_runtime_t *runtime = substream->runtime; | |
767 | emu10k1_pcm_t *epcm = runtime->private_data; | |
768 | int result = 0; | |
769 | ||
770 | spin_lock(&emu->reg_lock); | |
771 | switch (cmd) { | |
772 | case SNDRV_PCM_TRIGGER_START: | |
773 | // hmm this should cause full and half full interrupt to be raised? | |
774 | outl(epcm->capture_ipr, emu->port + IPR); | |
775 | snd_emu10k1_intr_enable(emu, epcm->capture_inte); | |
776 | // printk("adccr = 0x%x, adcbs = 0x%x\n", epcm->adccr, epcm->adcbs); | |
777 | switch (epcm->type) { | |
778 | case CAPTURE_AC97ADC: | |
779 | snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val); | |
780 | break; | |
781 | case CAPTURE_EFX: | |
782 | if (emu->audigy) { | |
783 | snd_emu10k1_ptr_write(emu, A_FXWC1, 0, epcm->capture_cr_val); | |
784 | snd_emu10k1_ptr_write(emu, A_FXWC2, 0, epcm->capture_cr_val2); | |
785 | } else | |
786 | snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val); | |
787 | break; | |
788 | default: | |
789 | break; | |
790 | } | |
791 | snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val); | |
792 | epcm->running = 1; | |
793 | epcm->first_ptr = 1; | |
794 | break; | |
795 | case SNDRV_PCM_TRIGGER_STOP: | |
796 | epcm->running = 0; | |
797 | snd_emu10k1_intr_disable(emu, epcm->capture_inte); | |
798 | outl(epcm->capture_ipr, emu->port + IPR); | |
799 | snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0); | |
800 | switch (epcm->type) { | |
801 | case CAPTURE_AC97ADC: | |
802 | snd_emu10k1_ptr_write(emu, ADCCR, 0, 0); | |
803 | break; | |
804 | case CAPTURE_EFX: | |
805 | if (emu->audigy) { | |
806 | snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0); | |
807 | snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0); | |
808 | } else | |
809 | snd_emu10k1_ptr_write(emu, FXWC, 0, 0); | |
810 | break; | |
811 | default: | |
812 | break; | |
813 | } | |
814 | break; | |
815 | default: | |
816 | result = -EINVAL; | |
817 | } | |
818 | spin_unlock(&emu->reg_lock); | |
819 | return result; | |
820 | } | |
821 | ||
822 | static snd_pcm_uframes_t snd_emu10k1_playback_pointer(snd_pcm_substream_t * substream) | |
823 | { | |
824 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
825 | snd_pcm_runtime_t *runtime = substream->runtime; | |
826 | emu10k1_pcm_t *epcm = runtime->private_data; | |
827 | unsigned int ptr; | |
828 | ||
829 | if (!epcm->running) | |
830 | return 0; | |
831 | ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff; | |
832 | #if 0 /* Perex's code */ | |
833 | ptr += runtime->buffer_size; | |
834 | ptr -= epcm->ccca_start_addr; | |
835 | ptr %= runtime->buffer_size; | |
836 | #else /* EMU10K1 Open Source code from Creative */ | |
837 | if (ptr < epcm->ccca_start_addr) | |
838 | ptr += runtime->buffer_size - epcm->ccca_start_addr; | |
839 | else { | |
840 | ptr -= epcm->ccca_start_addr; | |
841 | if (ptr >= runtime->buffer_size) | |
842 | ptr -= runtime->buffer_size; | |
843 | } | |
844 | #endif | |
845 | // printk("ptr = 0x%x, buffer_size = 0x%x, period_size = 0x%x\n", ptr, runtime->buffer_size, runtime->period_size); | |
846 | return ptr; | |
847 | } | |
848 | ||
849 | ||
850 | static int snd_emu10k1_efx_playback_trigger(snd_pcm_substream_t * substream, | |
851 | int cmd) | |
852 | { | |
853 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
854 | snd_pcm_runtime_t *runtime = substream->runtime; | |
855 | emu10k1_pcm_t *epcm = runtime->private_data; | |
856 | int i; | |
857 | int result = 0; | |
858 | ||
859 | spin_lock(&emu->reg_lock); | |
860 | switch (cmd) { | |
861 | case SNDRV_PCM_TRIGGER_START: | |
862 | // prepare voices | |
863 | for (i = 0; i < NUM_EFX_PLAYBACK; i++) { | |
864 | snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[i]); | |
865 | } | |
866 | snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra); | |
867 | ||
868 | /* follow thru */ | |
869 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
870 | snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL); | |
871 | snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0, | |
872 | &emu->efx_pcm_mixer[0]); | |
873 | for (i = 1; i < NUM_EFX_PLAYBACK; i++) | |
874 | snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0, | |
875 | &emu->efx_pcm_mixer[i]); | |
876 | snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0); | |
877 | snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1); | |
878 | for (i = 1; i < NUM_EFX_PLAYBACK; i++) | |
879 | snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0); | |
880 | epcm->running = 1; | |
881 | break; | |
882 | case SNDRV_PCM_TRIGGER_STOP: | |
883 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
884 | epcm->running = 0; | |
885 | for (i = 0; i < NUM_EFX_PLAYBACK; i++) { | |
886 | snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]); | |
887 | } | |
888 | snd_emu10k1_playback_stop_voice(emu, epcm->extra); | |
889 | break; | |
890 | default: | |
891 | result = -EINVAL; | |
892 | break; | |
893 | } | |
894 | spin_unlock(&emu->reg_lock); | |
895 | return result; | |
896 | } | |
897 | ||
898 | ||
899 | static snd_pcm_uframes_t snd_emu10k1_capture_pointer(snd_pcm_substream_t * substream) | |
900 | { | |
901 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
902 | snd_pcm_runtime_t *runtime = substream->runtime; | |
903 | emu10k1_pcm_t *epcm = runtime->private_data; | |
904 | unsigned int ptr; | |
905 | ||
906 | if (!epcm->running) | |
907 | return 0; | |
908 | if (epcm->first_ptr) { | |
909 | udelay(50); // hack, it takes awhile until capture is started | |
910 | epcm->first_ptr = 0; | |
911 | } | |
912 | ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff; | |
913 | return bytes_to_frames(runtime, ptr); | |
914 | } | |
915 | ||
916 | /* | |
917 | * Playback support device description | |
918 | */ | |
919 | ||
920 | static snd_pcm_hardware_t snd_emu10k1_playback = | |
921 | { | |
922 | .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | | |
923 | SNDRV_PCM_INFO_BLOCK_TRANSFER | | |
924 | SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE), | |
925 | .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE, | |
926 | .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000, | |
927 | .rate_min = 4000, | |
928 | .rate_max = 96000, | |
929 | .channels_min = 1, | |
930 | .channels_max = 2, | |
931 | .buffer_bytes_max = (128*1024), | |
932 | .period_bytes_min = 64, | |
933 | .period_bytes_max = (128*1024), | |
934 | .periods_min = 1, | |
935 | .periods_max = 1024, | |
936 | .fifo_size = 0, | |
937 | }; | |
938 | ||
939 | /* | |
940 | * Capture support device description | |
941 | */ | |
942 | ||
943 | static snd_pcm_hardware_t snd_emu10k1_capture = | |
944 | { | |
945 | .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | | |
946 | SNDRV_PCM_INFO_BLOCK_TRANSFER | | |
947 | SNDRV_PCM_INFO_MMAP_VALID), | |
948 | .formats = SNDRV_PCM_FMTBIT_S16_LE, | |
949 | .rates = SNDRV_PCM_RATE_8000_48000, | |
950 | .rate_min = 8000, | |
951 | .rate_max = 48000, | |
952 | .channels_min = 1, | |
953 | .channels_max = 2, | |
954 | .buffer_bytes_max = (64*1024), | |
955 | .period_bytes_min = 384, | |
956 | .period_bytes_max = (64*1024), | |
957 | .periods_min = 2, | |
958 | .periods_max = 2, | |
959 | .fifo_size = 0, | |
960 | }; | |
961 | ||
962 | /* | |
963 | * | |
964 | */ | |
965 | ||
966 | static void snd_emu10k1_pcm_mixer_notify1(emu10k1_t *emu, snd_kcontrol_t *kctl, int idx, int activate) | |
967 | { | |
968 | snd_ctl_elem_id_t id; | |
969 | ||
7c22f1aa TI |
970 | if (! kctl) |
971 | return; | |
1da177e4 LT |
972 | if (activate) |
973 | kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE; | |
974 | else | |
975 | kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE; | |
976 | snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE | | |
977 | SNDRV_CTL_EVENT_MASK_INFO, | |
978 | snd_ctl_build_ioff(&id, kctl, idx)); | |
979 | } | |
980 | ||
981 | static void snd_emu10k1_pcm_mixer_notify(emu10k1_t *emu, int idx, int activate) | |
982 | { | |
983 | snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate); | |
984 | snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate); | |
985 | snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate); | |
986 | } | |
987 | ||
988 | static void snd_emu10k1_pcm_efx_mixer_notify(emu10k1_t *emu, int idx, int activate) | |
989 | { | |
990 | snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate); | |
991 | snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate); | |
992 | snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate); | |
993 | } | |
994 | ||
995 | static void snd_emu10k1_pcm_free_substream(snd_pcm_runtime_t *runtime) | |
996 | { | |
4d572776 | 997 | kfree(runtime->private_data); |
1da177e4 LT |
998 | } |
999 | ||
1000 | static int snd_emu10k1_efx_playback_close(snd_pcm_substream_t * substream) | |
1001 | { | |
1002 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
1003 | emu10k1_pcm_mixer_t *mix; | |
1004 | int i; | |
1005 | ||
1006 | for (i=0; i < NUM_EFX_PLAYBACK; i++) { | |
1007 | mix = &emu->efx_pcm_mixer[i]; | |
1008 | mix->epcm = NULL; | |
1009 | snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0); | |
1010 | } | |
1011 | return 0; | |
1012 | } | |
1013 | ||
1014 | static int snd_emu10k1_efx_playback_open(snd_pcm_substream_t * substream) | |
1015 | { | |
1016 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
1017 | emu10k1_pcm_t *epcm; | |
1018 | emu10k1_pcm_mixer_t *mix; | |
1019 | snd_pcm_runtime_t *runtime = substream->runtime; | |
1020 | int i; | |
1021 | ||
e560d8d8 | 1022 | epcm = kzalloc(sizeof(*epcm), GFP_KERNEL); |
1da177e4 LT |
1023 | if (epcm == NULL) |
1024 | return -ENOMEM; | |
1025 | epcm->emu = emu; | |
1026 | epcm->type = PLAYBACK_EFX; | |
1027 | epcm->substream = substream; | |
1028 | ||
1029 | emu->pcm_playback_efx_substream = substream; | |
1030 | ||
1031 | runtime->private_data = epcm; | |
1032 | runtime->private_free = snd_emu10k1_pcm_free_substream; | |
1033 | runtime->hw = snd_emu10k1_efx_playback; | |
1034 | ||
1035 | for (i=0; i < NUM_EFX_PLAYBACK; i++) { | |
1036 | mix = &emu->efx_pcm_mixer[i]; | |
1037 | mix->send_routing[0][0] = i; | |
1038 | memset(&mix->send_volume, 0, sizeof(mix->send_volume)); | |
1039 | mix->send_volume[0][0] = 255; | |
1040 | mix->attn[0] = 0xffff; | |
1041 | mix->epcm = epcm; | |
1042 | snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1); | |
1043 | } | |
1044 | return 0; | |
1045 | } | |
1046 | ||
1047 | static int snd_emu10k1_playback_open(snd_pcm_substream_t * substream) | |
1048 | { | |
1049 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
1050 | emu10k1_pcm_t *epcm; | |
1051 | emu10k1_pcm_mixer_t *mix; | |
1052 | snd_pcm_runtime_t *runtime = substream->runtime; | |
1053 | int i, err; | |
1054 | ||
e560d8d8 | 1055 | epcm = kzalloc(sizeof(*epcm), GFP_KERNEL); |
1da177e4 LT |
1056 | if (epcm == NULL) |
1057 | return -ENOMEM; | |
1058 | epcm->emu = emu; | |
1059 | epcm->type = PLAYBACK_EMUVOICE; | |
1060 | epcm->substream = substream; | |
1061 | runtime->private_data = epcm; | |
1062 | runtime->private_free = snd_emu10k1_pcm_free_substream; | |
1063 | runtime->hw = snd_emu10k1_playback; | |
1064 | if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) { | |
1065 | kfree(epcm); | |
1066 | return err; | |
1067 | } | |
1068 | if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) { | |
1069 | kfree(epcm); | |
1070 | return err; | |
1071 | } | |
1072 | mix = &emu->pcm_mixer[substream->number]; | |
1073 | for (i = 0; i < 4; i++) | |
1074 | mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i; | |
1075 | memset(&mix->send_volume, 0, sizeof(mix->send_volume)); | |
1076 | mix->send_volume[0][0] = mix->send_volume[0][1] = | |
1077 | mix->send_volume[1][0] = mix->send_volume[2][1] = 255; | |
1078 | mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff; | |
1079 | mix->epcm = epcm; | |
1080 | snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1); | |
1081 | return 0; | |
1082 | } | |
1083 | ||
1084 | static int snd_emu10k1_playback_close(snd_pcm_substream_t * substream) | |
1085 | { | |
1086 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
1087 | emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[substream->number]; | |
1088 | ||
1089 | mix->epcm = NULL; | |
1090 | snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0); | |
1091 | return 0; | |
1092 | } | |
1093 | ||
1094 | static int snd_emu10k1_capture_open(snd_pcm_substream_t * substream) | |
1095 | { | |
1096 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
1097 | snd_pcm_runtime_t *runtime = substream->runtime; | |
1098 | emu10k1_pcm_t *epcm; | |
1099 | ||
e560d8d8 | 1100 | epcm = kzalloc(sizeof(*epcm), GFP_KERNEL); |
1da177e4 LT |
1101 | if (epcm == NULL) |
1102 | return -ENOMEM; | |
1103 | epcm->emu = emu; | |
1104 | epcm->type = CAPTURE_AC97ADC; | |
1105 | epcm->substream = substream; | |
1106 | epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL; | |
1107 | epcm->capture_inte = INTE_ADCBUFENABLE; | |
1108 | epcm->capture_ba_reg = ADCBA; | |
1109 | epcm->capture_bs_reg = ADCBS; | |
1110 | epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX; | |
1111 | runtime->private_data = epcm; | |
1112 | runtime->private_free = snd_emu10k1_pcm_free_substream; | |
1113 | runtime->hw = snd_emu10k1_capture; | |
1114 | emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt; | |
1115 | emu->pcm_capture_substream = substream; | |
1116 | snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes); | |
1117 | snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates); | |
1118 | return 0; | |
1119 | } | |
1120 | ||
1121 | static int snd_emu10k1_capture_close(snd_pcm_substream_t * substream) | |
1122 | { | |
1123 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
1124 | ||
1125 | emu->capture_interrupt = NULL; | |
1126 | emu->pcm_capture_substream = NULL; | |
1127 | return 0; | |
1128 | } | |
1129 | ||
1130 | static int snd_emu10k1_capture_mic_open(snd_pcm_substream_t * substream) | |
1131 | { | |
1132 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
1133 | emu10k1_pcm_t *epcm; | |
1134 | snd_pcm_runtime_t *runtime = substream->runtime; | |
1135 | ||
e560d8d8 | 1136 | epcm = kzalloc(sizeof(*epcm), GFP_KERNEL); |
1da177e4 LT |
1137 | if (epcm == NULL) |
1138 | return -ENOMEM; | |
1139 | epcm->emu = emu; | |
1140 | epcm->type = CAPTURE_AC97MIC; | |
1141 | epcm->substream = substream; | |
1142 | epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL; | |
1143 | epcm->capture_inte = INTE_MICBUFENABLE; | |
1144 | epcm->capture_ba_reg = MICBA; | |
1145 | epcm->capture_bs_reg = MICBS; | |
1146 | epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX; | |
1147 | substream->runtime->private_data = epcm; | |
1148 | substream->runtime->private_free = snd_emu10k1_pcm_free_substream; | |
1149 | runtime->hw = snd_emu10k1_capture; | |
1150 | runtime->hw.rates = SNDRV_PCM_RATE_8000; | |
1151 | runtime->hw.rate_min = runtime->hw.rate_max = 8000; | |
1152 | runtime->hw.channels_min = 1; | |
1153 | emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt; | |
1154 | emu->pcm_capture_mic_substream = substream; | |
1155 | snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes); | |
1156 | return 0; | |
1157 | } | |
1158 | ||
1159 | static int snd_emu10k1_capture_mic_close(snd_pcm_substream_t * substream) | |
1160 | { | |
1161 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
1162 | ||
1163 | emu->capture_interrupt = NULL; | |
1164 | emu->pcm_capture_mic_substream = NULL; | |
1165 | return 0; | |
1166 | } | |
1167 | ||
1168 | static int snd_emu10k1_capture_efx_open(snd_pcm_substream_t * substream) | |
1169 | { | |
1170 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
1171 | emu10k1_pcm_t *epcm; | |
1172 | snd_pcm_runtime_t *runtime = substream->runtime; | |
1173 | int nefx = emu->audigy ? 64 : 32; | |
1174 | int idx; | |
1175 | ||
e560d8d8 | 1176 | epcm = kzalloc(sizeof(*epcm), GFP_KERNEL); |
1da177e4 LT |
1177 | if (epcm == NULL) |
1178 | return -ENOMEM; | |
1179 | epcm->emu = emu; | |
1180 | epcm->type = CAPTURE_EFX; | |
1181 | epcm->substream = substream; | |
1182 | epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL; | |
1183 | epcm->capture_inte = INTE_EFXBUFENABLE; | |
1184 | epcm->capture_ba_reg = FXBA; | |
1185 | epcm->capture_bs_reg = FXBS; | |
1186 | epcm->capture_idx_reg = FXIDX; | |
1187 | substream->runtime->private_data = epcm; | |
1188 | substream->runtime->private_free = snd_emu10k1_pcm_free_substream; | |
1189 | runtime->hw = snd_emu10k1_capture; | |
1190 | runtime->hw.rates = SNDRV_PCM_RATE_48000; | |
1191 | runtime->hw.rate_min = runtime->hw.rate_max = 48000; | |
1192 | spin_lock_irq(&emu->reg_lock); | |
1193 | runtime->hw.channels_min = runtime->hw.channels_max = 0; | |
1194 | for (idx = 0; idx < nefx; idx++) { | |
1195 | if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) { | |
1196 | runtime->hw.channels_min++; | |
1197 | runtime->hw.channels_max++; | |
1198 | } | |
1199 | } | |
1200 | epcm->capture_cr_val = emu->efx_voices_mask[0]; | |
1201 | epcm->capture_cr_val2 = emu->efx_voices_mask[1]; | |
1202 | spin_unlock_irq(&emu->reg_lock); | |
1203 | emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt; | |
1204 | emu->pcm_capture_efx_substream = substream; | |
1205 | snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes); | |
1206 | return 0; | |
1207 | } | |
1208 | ||
1209 | static int snd_emu10k1_capture_efx_close(snd_pcm_substream_t * substream) | |
1210 | { | |
1211 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
1212 | ||
1213 | emu->capture_interrupt = NULL; | |
1214 | emu->pcm_capture_efx_substream = NULL; | |
1215 | return 0; | |
1216 | } | |
1217 | ||
1218 | static snd_pcm_ops_t snd_emu10k1_playback_ops = { | |
1219 | .open = snd_emu10k1_playback_open, | |
1220 | .close = snd_emu10k1_playback_close, | |
1221 | .ioctl = snd_pcm_lib_ioctl, | |
1222 | .hw_params = snd_emu10k1_playback_hw_params, | |
1223 | .hw_free = snd_emu10k1_playback_hw_free, | |
1224 | .prepare = snd_emu10k1_playback_prepare, | |
1225 | .trigger = snd_emu10k1_playback_trigger, | |
1226 | .pointer = snd_emu10k1_playback_pointer, | |
1227 | .page = snd_pcm_sgbuf_ops_page, | |
1228 | }; | |
1229 | ||
1230 | static snd_pcm_ops_t snd_emu10k1_capture_ops = { | |
1231 | .open = snd_emu10k1_capture_open, | |
1232 | .close = snd_emu10k1_capture_close, | |
1233 | .ioctl = snd_pcm_lib_ioctl, | |
1234 | .hw_params = snd_emu10k1_capture_hw_params, | |
1235 | .hw_free = snd_emu10k1_capture_hw_free, | |
1236 | .prepare = snd_emu10k1_capture_prepare, | |
1237 | .trigger = snd_emu10k1_capture_trigger, | |
1238 | .pointer = snd_emu10k1_capture_pointer, | |
1239 | }; | |
1240 | ||
1241 | /* EFX playback */ | |
1242 | static snd_pcm_ops_t snd_emu10k1_efx_playback_ops = { | |
1243 | .open = snd_emu10k1_efx_playback_open, | |
1244 | .close = snd_emu10k1_efx_playback_close, | |
1245 | .ioctl = snd_pcm_lib_ioctl, | |
1246 | .hw_params = snd_emu10k1_playback_hw_params, | |
1247 | .hw_free = snd_emu10k1_efx_playback_hw_free, | |
1248 | .prepare = snd_emu10k1_efx_playback_prepare, | |
1249 | .trigger = snd_emu10k1_efx_playback_trigger, | |
1250 | .pointer = snd_emu10k1_efx_playback_pointer, | |
1251 | .page = snd_pcm_sgbuf_ops_page, | |
1252 | }; | |
1253 | ||
1da177e4 LT |
1254 | int __devinit snd_emu10k1_pcm(emu10k1_t * emu, int device, snd_pcm_t ** rpcm) |
1255 | { | |
1256 | snd_pcm_t *pcm; | |
1257 | snd_pcm_substream_t *substream; | |
1258 | int err; | |
1259 | ||
1260 | if (rpcm) | |
1261 | *rpcm = NULL; | |
1262 | ||
1263 | if ((err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm)) < 0) | |
1264 | return err; | |
1265 | ||
1266 | pcm->private_data = emu; | |
1da177e4 LT |
1267 | |
1268 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops); | |
1269 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops); | |
1270 | ||
1271 | pcm->info_flags = 0; | |
1272 | pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX; | |
1273 | strcpy(pcm->name, "ADC Capture/Standard PCM Playback"); | |
1274 | emu->pcm = pcm; | |
1275 | ||
1276 | for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) | |
1277 | if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0) | |
1278 | return err; | |
1279 | ||
1280 | for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next) | |
1281 | snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024); | |
1282 | ||
1283 | if (rpcm) | |
1284 | *rpcm = pcm; | |
1285 | ||
1286 | return 0; | |
1287 | } | |
1288 | ||
1289 | int __devinit snd_emu10k1_pcm_multi(emu10k1_t * emu, int device, snd_pcm_t ** rpcm) | |
1290 | { | |
1291 | snd_pcm_t *pcm; | |
1292 | snd_pcm_substream_t *substream; | |
1293 | int err; | |
1294 | ||
1295 | if (rpcm) | |
1296 | *rpcm = NULL; | |
1297 | ||
1298 | if ((err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm)) < 0) | |
1299 | return err; | |
1300 | ||
1301 | pcm->private_data = emu; | |
1da177e4 LT |
1302 | |
1303 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops); | |
1304 | ||
1305 | pcm->info_flags = 0; | |
1306 | pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX; | |
1307 | strcpy(pcm->name, "Multichannel Playback"); | |
1308 | emu->pcm = pcm; | |
1309 | ||
1310 | for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) | |
1311 | if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0) | |
1312 | return err; | |
1313 | ||
1314 | if (rpcm) | |
1315 | *rpcm = pcm; | |
1316 | ||
1317 | return 0; | |
1318 | } | |
1319 | ||
1320 | ||
1321 | static snd_pcm_ops_t snd_emu10k1_capture_mic_ops = { | |
1322 | .open = snd_emu10k1_capture_mic_open, | |
1323 | .close = snd_emu10k1_capture_mic_close, | |
1324 | .ioctl = snd_pcm_lib_ioctl, | |
1325 | .hw_params = snd_emu10k1_capture_hw_params, | |
1326 | .hw_free = snd_emu10k1_capture_hw_free, | |
1327 | .prepare = snd_emu10k1_capture_prepare, | |
1328 | .trigger = snd_emu10k1_capture_trigger, | |
1329 | .pointer = snd_emu10k1_capture_pointer, | |
1330 | }; | |
1331 | ||
1da177e4 LT |
1332 | int __devinit snd_emu10k1_pcm_mic(emu10k1_t * emu, int device, snd_pcm_t ** rpcm) |
1333 | { | |
1334 | snd_pcm_t *pcm; | |
1335 | int err; | |
1336 | ||
1337 | if (rpcm) | |
1338 | *rpcm = NULL; | |
1339 | ||
1340 | if ((err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm)) < 0) | |
1341 | return err; | |
1342 | ||
1343 | pcm->private_data = emu; | |
1da177e4 LT |
1344 | |
1345 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops); | |
1346 | ||
1347 | pcm->info_flags = 0; | |
1348 | strcpy(pcm->name, "Mic Capture"); | |
1349 | emu->pcm_mic = pcm; | |
1350 | ||
1351 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024); | |
1352 | ||
1353 | if (rpcm) | |
1354 | *rpcm = pcm; | |
1355 | return 0; | |
1356 | } | |
1357 | ||
1358 | static int snd_emu10k1_pcm_efx_voices_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo) | |
1359 | { | |
1360 | emu10k1_t *emu = snd_kcontrol_chip(kcontrol); | |
1361 | int nefx = emu->audigy ? 64 : 32; | |
1362 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; | |
1363 | uinfo->count = nefx; | |
1364 | uinfo->value.integer.min = 0; | |
1365 | uinfo->value.integer.max = 1; | |
1366 | return 0; | |
1367 | } | |
1368 | ||
1369 | static int snd_emu10k1_pcm_efx_voices_mask_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol) | |
1370 | { | |
1371 | emu10k1_t *emu = snd_kcontrol_chip(kcontrol); | |
1372 | int nefx = emu->audigy ? 64 : 32; | |
1373 | int idx; | |
1374 | ||
1375 | spin_lock_irq(&emu->reg_lock); | |
1376 | for (idx = 0; idx < nefx; idx++) | |
1377 | ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0; | |
1378 | spin_unlock_irq(&emu->reg_lock); | |
1379 | return 0; | |
1380 | } | |
1381 | ||
1382 | static int snd_emu10k1_pcm_efx_voices_mask_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol) | |
1383 | { | |
1384 | emu10k1_t *emu = snd_kcontrol_chip(kcontrol); | |
1385 | unsigned int nval[2], bits; | |
1386 | int nefx = emu->audigy ? 64 : 32; | |
1387 | int nefxb = emu->audigy ? 7 : 6; | |
1388 | int change, idx; | |
1389 | ||
1390 | nval[0] = nval[1] = 0; | |
1391 | for (idx = 0, bits = 0; idx < nefx; idx++) | |
1392 | if (ucontrol->value.integer.value[idx]) { | |
1393 | nval[idx / 32] |= 1 << (idx % 32); | |
1394 | bits++; | |
1395 | } | |
1396 | ||
1397 | for (idx = 0; idx < nefxb; idx++) | |
1398 | if (1 << idx == bits) | |
1399 | break; | |
1400 | ||
1401 | if (idx >= nefxb) | |
1402 | return -EINVAL; | |
1403 | ||
1404 | spin_lock_irq(&emu->reg_lock); | |
1405 | change = (nval[0] != emu->efx_voices_mask[0]) || | |
1406 | (nval[1] != emu->efx_voices_mask[1]); | |
1407 | emu->efx_voices_mask[0] = nval[0]; | |
1408 | emu->efx_voices_mask[1] = nval[1]; | |
1409 | spin_unlock_irq(&emu->reg_lock); | |
1410 | return change; | |
1411 | } | |
1412 | ||
1413 | static snd_kcontrol_new_t snd_emu10k1_pcm_efx_voices_mask = { | |
1414 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, | |
1415 | .name = "Captured FX8010 Outputs", | |
1416 | .info = snd_emu10k1_pcm_efx_voices_mask_info, | |
1417 | .get = snd_emu10k1_pcm_efx_voices_mask_get, | |
1418 | .put = snd_emu10k1_pcm_efx_voices_mask_put | |
1419 | }; | |
1420 | ||
1421 | static snd_pcm_ops_t snd_emu10k1_capture_efx_ops = { | |
1422 | .open = snd_emu10k1_capture_efx_open, | |
1423 | .close = snd_emu10k1_capture_efx_close, | |
1424 | .ioctl = snd_pcm_lib_ioctl, | |
1425 | .hw_params = snd_emu10k1_capture_hw_params, | |
1426 | .hw_free = snd_emu10k1_capture_hw_free, | |
1427 | .prepare = snd_emu10k1_capture_prepare, | |
1428 | .trigger = snd_emu10k1_capture_trigger, | |
1429 | .pointer = snd_emu10k1_capture_pointer, | |
1430 | }; | |
1431 | ||
1432 | ||
1433 | /* EFX playback */ | |
1434 | ||
1435 | #define INITIAL_TRAM_SHIFT 14 | |
1436 | #define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1) | |
1437 | ||
1438 | static void snd_emu10k1_fx8010_playback_irq(emu10k1_t *emu, void *private_data) | |
1439 | { | |
1440 | snd_pcm_substream_t *substream = private_data; | |
1441 | snd_pcm_period_elapsed(substream); | |
1442 | } | |
1443 | ||
1444 | static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left, | |
1445 | unsigned short *dst_right, | |
1446 | unsigned short *src, | |
1447 | unsigned int count, | |
1448 | unsigned int tram_shift) | |
1449 | { | |
1450 | // printk("tram_poke1: dst_left = 0x%p, dst_right = 0x%p, src = 0x%p, count = 0x%x\n", dst_left, dst_right, src, count); | |
1451 | if ((tram_shift & 1) == 0) { | |
1452 | while (count--) { | |
1453 | *dst_left-- = *src++; | |
1454 | *dst_right-- = *src++; | |
1455 | } | |
1456 | } else { | |
1457 | while (count--) { | |
1458 | *dst_right-- = *src++; | |
1459 | *dst_left-- = *src++; | |
1460 | } | |
1461 | } | |
1462 | } | |
1463 | ||
1464 | static void fx8010_pb_trans_copy(snd_pcm_substream_t *substream, | |
1465 | snd_pcm_indirect_t *rec, size_t bytes) | |
1466 | { | |
1467 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
1468 | snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number]; | |
1469 | unsigned int tram_size = pcm->buffer_size; | |
1470 | unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data); | |
1471 | unsigned int frames = bytes >> 2, count; | |
1472 | unsigned int tram_pos = pcm->tram_pos; | |
1473 | unsigned int tram_shift = pcm->tram_shift; | |
1474 | ||
1475 | while (frames > tram_pos) { | |
1476 | count = tram_pos + 1; | |
1477 | snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos, | |
1478 | (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2, | |
1479 | src, count, tram_shift); | |
1480 | src += count * 2; | |
1481 | frames -= count; | |
1482 | tram_pos = (tram_size / 2) - 1; | |
1483 | tram_shift++; | |
1484 | } | |
1485 | snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos, | |
1486 | (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2, | |
1487 | src, frames, tram_shift); | |
1488 | tram_pos -= frames; | |
1489 | pcm->tram_pos = tram_pos; | |
1490 | pcm->tram_shift = tram_shift; | |
1491 | } | |
1492 | ||
1493 | static int snd_emu10k1_fx8010_playback_transfer(snd_pcm_substream_t *substream) | |
1494 | { | |
1495 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
1496 | snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number]; | |
1497 | ||
1498 | snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec, fx8010_pb_trans_copy); | |
1499 | return 0; | |
1500 | } | |
1501 | ||
1502 | static int snd_emu10k1_fx8010_playback_hw_params(snd_pcm_substream_t * substream, | |
1503 | snd_pcm_hw_params_t * hw_params) | |
1504 | { | |
1505 | return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)); | |
1506 | } | |
1507 | ||
1508 | static int snd_emu10k1_fx8010_playback_hw_free(snd_pcm_substream_t * substream) | |
1509 | { | |
1510 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
1511 | snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number]; | |
1512 | unsigned int i; | |
1513 | ||
1514 | for (i = 0; i < pcm->channels; i++) | |
1515 | snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0); | |
1516 | snd_pcm_lib_free_pages(substream); | |
1517 | return 0; | |
1518 | } | |
1519 | ||
1520 | static int snd_emu10k1_fx8010_playback_prepare(snd_pcm_substream_t * substream) | |
1521 | { | |
1522 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
1523 | snd_pcm_runtime_t *runtime = substream->runtime; | |
1524 | snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number]; | |
1525 | unsigned int i; | |
1526 | ||
1527 | // printk("prepare: etram_pages = 0x%p, dma_area = 0x%x, buffer_size = 0x%x (0x%x)\n", emu->fx8010.etram_pages, runtime->dma_area, runtime->buffer_size, runtime->buffer_size << 2); | |
1528 | memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec)); | |
1529 | pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */ | |
1530 | pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream); | |
1531 | pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size); | |
1532 | pcm->tram_shift = 0; | |
1533 | snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0); /* reset */ | |
1534 | snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0); /* reset */ | |
1535 | snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size); | |
1536 | snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0); /* reset ptr number */ | |
1537 | snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size); | |
1538 | snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size); | |
1539 | for (i = 0; i < pcm->channels; i++) | |
1540 | snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels)); | |
1541 | return 0; | |
1542 | } | |
1543 | ||
1544 | static int snd_emu10k1_fx8010_playback_trigger(snd_pcm_substream_t * substream, int cmd) | |
1545 | { | |
1546 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
1547 | snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number]; | |
1548 | int result = 0; | |
1549 | ||
1550 | spin_lock(&emu->reg_lock); | |
1551 | switch (cmd) { | |
1552 | case SNDRV_PCM_TRIGGER_START: | |
1553 | /* follow thru */ | |
1554 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
1555 | #ifdef EMU10K1_SET_AC3_IEC958 | |
1556 | { | |
1557 | int i; | |
1558 | for (i = 0; i < 3; i++) { | |
1559 | unsigned int bits; | |
1560 | bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 | | |
1561 | SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS | | |
1562 | 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA; | |
1563 | snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits); | |
1564 | } | |
1565 | } | |
1566 | #endif | |
1567 | result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq); | |
1568 | if (result < 0) | |
1569 | goto __err; | |
1570 | snd_emu10k1_fx8010_playback_transfer(substream); /* roll the ball */ | |
1571 | snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1); | |
1572 | break; | |
1573 | case SNDRV_PCM_TRIGGER_STOP: | |
1574 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
1575 | snd_emu10k1_fx8010_unregister_irq_handler(emu, pcm->irq); pcm->irq = NULL; | |
1576 | snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0); | |
1577 | pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size); | |
1578 | pcm->tram_shift = 0; | |
1579 | break; | |
1580 | default: | |
1581 | result = -EINVAL; | |
1582 | break; | |
1583 | } | |
1584 | __err: | |
1585 | spin_unlock(&emu->reg_lock); | |
1586 | return result; | |
1587 | } | |
1588 | ||
1589 | static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(snd_pcm_substream_t * substream) | |
1590 | { | |
1591 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
1592 | snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number]; | |
1593 | size_t ptr; /* byte pointer */ | |
1594 | ||
1595 | if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0)) | |
1596 | return 0; | |
1597 | ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2; | |
1598 | return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr); | |
1599 | } | |
1600 | ||
1601 | static snd_pcm_hardware_t snd_emu10k1_fx8010_playback = | |
1602 | { | |
1603 | .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | | |
1604 | /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE), | |
1605 | .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE, | |
1606 | .rates = SNDRV_PCM_RATE_48000, | |
1607 | .rate_min = 48000, | |
1608 | .rate_max = 48000, | |
1609 | .channels_min = 1, | |
1610 | .channels_max = 1, | |
1611 | .buffer_bytes_max = (128*1024), | |
1612 | .period_bytes_min = 1024, | |
1613 | .period_bytes_max = (128*1024), | |
1614 | .periods_min = 1, | |
1615 | .periods_max = 1024, | |
1616 | .fifo_size = 0, | |
1617 | }; | |
1618 | ||
1619 | static int snd_emu10k1_fx8010_playback_open(snd_pcm_substream_t * substream) | |
1620 | { | |
1621 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
1622 | snd_pcm_runtime_t *runtime = substream->runtime; | |
1623 | snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number]; | |
1624 | ||
1625 | runtime->hw = snd_emu10k1_fx8010_playback; | |
1626 | runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels; | |
1627 | runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2; | |
1628 | spin_lock_irq(&emu->reg_lock); | |
1629 | if (pcm->valid == 0) { | |
1630 | spin_unlock_irq(&emu->reg_lock); | |
1631 | return -ENODEV; | |
1632 | } | |
1633 | pcm->opened = 1; | |
1634 | spin_unlock_irq(&emu->reg_lock); | |
1635 | return 0; | |
1636 | } | |
1637 | ||
1638 | static int snd_emu10k1_fx8010_playback_close(snd_pcm_substream_t * substream) | |
1639 | { | |
1640 | emu10k1_t *emu = snd_pcm_substream_chip(substream); | |
1641 | snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number]; | |
1642 | ||
1643 | spin_lock_irq(&emu->reg_lock); | |
1644 | pcm->opened = 0; | |
1645 | spin_unlock_irq(&emu->reg_lock); | |
1646 | return 0; | |
1647 | } | |
1648 | ||
1649 | static snd_pcm_ops_t snd_emu10k1_fx8010_playback_ops = { | |
1650 | .open = snd_emu10k1_fx8010_playback_open, | |
1651 | .close = snd_emu10k1_fx8010_playback_close, | |
1652 | .ioctl = snd_pcm_lib_ioctl, | |
1653 | .hw_params = snd_emu10k1_fx8010_playback_hw_params, | |
1654 | .hw_free = snd_emu10k1_fx8010_playback_hw_free, | |
1655 | .prepare = snd_emu10k1_fx8010_playback_prepare, | |
1656 | .trigger = snd_emu10k1_fx8010_playback_trigger, | |
1657 | .pointer = snd_emu10k1_fx8010_playback_pointer, | |
1658 | .ack = snd_emu10k1_fx8010_playback_transfer, | |
1659 | }; | |
1660 | ||
1da177e4 LT |
1661 | int __devinit snd_emu10k1_pcm_efx(emu10k1_t * emu, int device, snd_pcm_t ** rpcm) |
1662 | { | |
1663 | snd_pcm_t *pcm; | |
67ed4161 | 1664 | snd_kcontrol_t *kctl; |
1da177e4 LT |
1665 | int err; |
1666 | ||
1667 | if (rpcm) | |
1668 | *rpcm = NULL; | |
1669 | ||
1670 | if ((err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm)) < 0) | |
1671 | return err; | |
1672 | ||
1673 | pcm->private_data = emu; | |
1da177e4 LT |
1674 | |
1675 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops); | |
1676 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops); | |
1677 | ||
1678 | pcm->info_flags = 0; | |
1679 | strcpy(pcm->name, "Multichannel Capture/PT Playback"); | |
1680 | emu->pcm_efx = pcm; | |
1681 | if (rpcm) | |
1682 | *rpcm = pcm; | |
1683 | ||
1684 | /* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs | |
1685 | * to these | |
1686 | */ | |
1687 | ||
1688 | /* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */ | |
1689 | if (emu->audigy) { | |
1690 | emu->efx_voices_mask[0] = 0; | |
1691 | emu->efx_voices_mask[1] = 0xffff; | |
1692 | } else { | |
1693 | emu->efx_voices_mask[0] = 0xffff0000; | |
1694 | emu->efx_voices_mask[1] = 0; | |
1695 | } | |
67ed4161 CL |
1696 | kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu); |
1697 | if (!kctl) | |
1698 | return -ENOMEM; | |
1699 | kctl->id.device = device; | |
1700 | snd_ctl_add(emu->card, kctl); | |
1da177e4 LT |
1701 | |
1702 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024); | |
1703 | ||
1704 | return 0; | |
1705 | } |