1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 * Lee Revell <rlrevell@joe-job.com>
5 * James Courtier-Dutton <James@superbug.co.uk>
6 * Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
7 * Creative Labs, Inc.
8 *
9 * Routines for control of EMU10K1 chips / PCM routines
10 */
11
12 #include <linux/pci.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/time.h>
16 #include <linux/init.h>
17 #include <sound/core.h>
18 #include <sound/emu10k1.h>
19
snd_emu10k1_pcm_interrupt(struct snd_emu10k1 * emu,struct snd_emu10k1_voice * voice)20 static void snd_emu10k1_pcm_interrupt(struct snd_emu10k1 *emu,
21 struct snd_emu10k1_voice *voice)
22 {
23 struct snd_emu10k1_pcm *epcm;
24
25 epcm = voice->epcm;
26 if (!epcm)
27 return;
28 if (epcm->substream == NULL)
29 return;
30 #if 0
31 dev_dbg(emu->card->dev,
32 "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
33 epcm->substream->runtime->hw->pointer(emu, epcm->substream),
34 snd_pcm_lib_period_bytes(epcm->substream),
35 snd_pcm_lib_buffer_bytes(epcm->substream));
36 #endif
37 snd_pcm_period_elapsed(epcm->substream);
38 }
39
snd_emu10k1_pcm_ac97adc_interrupt(struct snd_emu10k1 * emu,unsigned int status)40 static void snd_emu10k1_pcm_ac97adc_interrupt(struct snd_emu10k1 *emu,
41 unsigned int status)
42 {
43 #if 0
44 if (status & IPR_ADCBUFHALFFULL) {
45 if (emu->pcm_capture_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
46 return;
47 }
48 #endif
49 snd_pcm_period_elapsed(emu->pcm_capture_substream);
50 }
51
snd_emu10k1_pcm_ac97mic_interrupt(struct snd_emu10k1 * emu,unsigned int status)52 static void snd_emu10k1_pcm_ac97mic_interrupt(struct snd_emu10k1 *emu,
53 unsigned int status)
54 {
55 #if 0
56 if (status & IPR_MICBUFHALFFULL) {
57 if (emu->pcm_capture_mic_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
58 return;
59 }
60 #endif
61 snd_pcm_period_elapsed(emu->pcm_capture_mic_substream);
62 }
63
snd_emu10k1_pcm_efx_interrupt(struct snd_emu10k1 * emu,unsigned int status)64 static void snd_emu10k1_pcm_efx_interrupt(struct snd_emu10k1 *emu,
65 unsigned int status)
66 {
67 #if 0
68 if (status & IPR_EFXBUFHALFFULL) {
69 if (emu->pcm_capture_efx_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
70 return;
71 }
72 #endif
73 snd_pcm_period_elapsed(emu->pcm_capture_efx_substream);
74 }
75
snd_emu10k1_pcm_free_voices(struct snd_emu10k1_pcm * epcm)76 static void snd_emu10k1_pcm_free_voices(struct snd_emu10k1_pcm *epcm)
77 {
78 for (unsigned i = 0; i < ARRAY_SIZE(epcm->voices); i++) {
79 if (epcm->voices[i]) {
80 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
81 epcm->voices[i] = NULL;
82 }
83 }
84 }
85
snd_emu10k1_pcm_channel_alloc(struct snd_emu10k1_pcm * epcm,int type,int count,int channels)86 static int snd_emu10k1_pcm_channel_alloc(struct snd_emu10k1_pcm *epcm,
87 int type, int count, int channels)
88 {
89 int err;
90
91 snd_emu10k1_pcm_free_voices(epcm);
92
93 err = snd_emu10k1_voice_alloc(epcm->emu,
94 type, count, channels,
95 epcm, &epcm->voices[0]);
96 if (err < 0)
97 return err;
98
99 if (epcm->extra == NULL) {
100 // The hardware supports only (half-)loop interrupts, so to support an
101 // arbitrary number of periods per buffer, we use an extra voice with a
102 // period-sized loop as the interrupt source. Additionally, the interrupt
103 // timing of the hardware is "suboptimal" and needs some compensation.
104 err = snd_emu10k1_voice_alloc(epcm->emu,
105 type + 1, 1, 1,
106 epcm, &epcm->extra);
107 if (err < 0) {
108 /*
109 dev_dbg(emu->card->dev, "pcm_channel_alloc: "
110 "failed extra: voices=%d, frame=%d\n",
111 voices, frame);
112 */
113 snd_emu10k1_pcm_free_voices(epcm);
114 return err;
115 }
116 epcm->extra->interrupt = snd_emu10k1_pcm_interrupt;
117 }
118
119 return 0;
120 }
121
122 // Primes 2-7 and 2^n multiples thereof, up to 16.
123 static const unsigned int efx_capture_channels[] = {
124 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16
125 };
126
127 static const struct snd_pcm_hw_constraint_list hw_constraints_efx_capture_channels = {
128 .count = ARRAY_SIZE(efx_capture_channels),
129 .list = efx_capture_channels,
130 .mask = 0
131 };
132
133 static const unsigned int capture_buffer_sizes[31] = {
134 384, 448, 512, 640,
135 384*2, 448*2, 512*2, 640*2,
136 384*4, 448*4, 512*4, 640*4,
137 384*8, 448*8, 512*8, 640*8,
138 384*16, 448*16, 512*16, 640*16,
139 384*32, 448*32, 512*32, 640*32,
140 384*64, 448*64, 512*64, 640*64,
141 384*128,448*128,512*128
142 };
143
144 static const struct snd_pcm_hw_constraint_list hw_constraints_capture_buffer_sizes = {
145 .count = 31,
146 .list = capture_buffer_sizes,
147 .mask = 0
148 };
149
snd_emu10k1_capture_rate_reg(unsigned int rate)150 static unsigned int snd_emu10k1_capture_rate_reg(unsigned int rate)
151 {
152 switch (rate) {
153 case 8000: return ADCCR_SAMPLERATE_8;
154 case 11025: return ADCCR_SAMPLERATE_11;
155 case 16000: return ADCCR_SAMPLERATE_16;
156 case 22050: return ADCCR_SAMPLERATE_22;
157 case 24000: return ADCCR_SAMPLERATE_24;
158 case 32000: return ADCCR_SAMPLERATE_32;
159 case 44100: return ADCCR_SAMPLERATE_44;
160 case 48000: return ADCCR_SAMPLERATE_48;
161 default:
162 snd_BUG();
163 return ADCCR_SAMPLERATE_8;
164 }
165 }
166
snd_emu10k1_audigy_capture_rate_reg(unsigned int rate)167 static unsigned int snd_emu10k1_audigy_capture_rate_reg(unsigned int rate)
168 {
169 switch (rate) {
170 case 8000: return A_ADCCR_SAMPLERATE_8;
171 case 11025: return A_ADCCR_SAMPLERATE_11;
172 case 12000: return A_ADCCR_SAMPLERATE_12;
173 case 16000: return ADCCR_SAMPLERATE_16;
174 case 22050: return ADCCR_SAMPLERATE_22;
175 case 24000: return ADCCR_SAMPLERATE_24;
176 case 32000: return ADCCR_SAMPLERATE_32;
177 case 44100: return ADCCR_SAMPLERATE_44;
178 case 48000: return ADCCR_SAMPLERATE_48;
179 default:
180 snd_BUG();
181 return A_ADCCR_SAMPLERATE_8;
182 }
183 }
184
snd_emu10k1_constrain_capture_rates(struct snd_emu10k1 * emu,struct snd_pcm_runtime * runtime)185 static void snd_emu10k1_constrain_capture_rates(struct snd_emu10k1 *emu,
186 struct snd_pcm_runtime *runtime)
187 {
188 if (emu->card_capabilities->emu_model &&
189 emu->emu1010.word_clock == 44100) {
190 runtime->hw.rates = SNDRV_PCM_RATE_11025 | \
191 SNDRV_PCM_RATE_22050 | \
192 SNDRV_PCM_RATE_44100;
193 runtime->hw.rate_min = 11025;
194 runtime->hw.rate_max = 44100;
195 } else if (emu->audigy) {
196 runtime->hw.rates = SNDRV_PCM_RATE_8000_48000 |
197 SNDRV_PCM_RATE_12000 |
198 SNDRV_PCM_RATE_24000;
199 }
200 }
201
snd_emu1010_constrain_efx_rate(struct snd_emu10k1 * emu,struct snd_pcm_runtime * runtime)202 static void snd_emu1010_constrain_efx_rate(struct snd_emu10k1 *emu,
203 struct snd_pcm_runtime *runtime)
204 {
205 int rate;
206
207 rate = emu->emu1010.word_clock;
208 runtime->hw.rate_min = runtime->hw.rate_max = rate;
209 runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
210 }
211
emu10k1_calc_pitch_target(unsigned int rate)212 static unsigned int emu10k1_calc_pitch_target(unsigned int rate)
213 {
214 unsigned int pitch_target;
215
216 pitch_target = (rate << 8) / 375;
217 pitch_target = (pitch_target >> 1) + (pitch_target & 1);
218 return pitch_target;
219 }
220
221 #define PITCH_48000 0x00004000
222 #define PITCH_96000 0x00008000
223 #define PITCH_85000 0x00007155
224 #define PITCH_80726 0x00006ba2
225 #define PITCH_67882 0x00005a82
226 #define PITCH_57081 0x00004c1c
227
emu10k1_select_interprom(unsigned int pitch_target)228 static unsigned int emu10k1_select_interprom(unsigned int pitch_target)
229 {
230 if (pitch_target == PITCH_48000)
231 return CCCA_INTERPROM_0;
232 else if (pitch_target < PITCH_48000)
233 return CCCA_INTERPROM_1;
234 else if (pitch_target >= PITCH_96000)
235 return CCCA_INTERPROM_0;
236 else if (pitch_target >= PITCH_85000)
237 return CCCA_INTERPROM_6;
238 else if (pitch_target >= PITCH_80726)
239 return CCCA_INTERPROM_5;
240 else if (pitch_target >= PITCH_67882)
241 return CCCA_INTERPROM_4;
242 else if (pitch_target >= PITCH_57081)
243 return CCCA_INTERPROM_3;
244 else
245 return CCCA_INTERPROM_2;
246 }
247
emu10k1_send_target_from_amount(u8 amount)248 static u16 emu10k1_send_target_from_amount(u8 amount)
249 {
250 static const u8 shifts[8] = { 4, 4, 5, 6, 7, 8, 9, 10 };
251 static const u16 offsets[8] = { 0, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000 };
252 u8 exp;
253
254 if (amount == 0xff)
255 return 0xffff;
256 exp = amount >> 5;
257 return ((amount & 0x1f) << shifts[exp]) + offsets[exp];
258 }
259
snd_emu10k1_pcm_init_voice(struct snd_emu10k1 * emu,struct snd_emu10k1_voice * evoice,bool w_16,bool stereo,unsigned int start_addr,unsigned int end_addr,const unsigned char * send_routing,const unsigned char * send_amount)260 static void snd_emu10k1_pcm_init_voice(struct snd_emu10k1 *emu,
261 struct snd_emu10k1_voice *evoice,
262 bool w_16, bool stereo,
263 unsigned int start_addr,
264 unsigned int end_addr,
265 const unsigned char *send_routing,
266 const unsigned char *send_amount)
267 {
268 unsigned int silent_page;
269 int voice;
270
271 voice = evoice->number;
272
273 silent_page = ((unsigned int)emu->silent_page.addr << emu->address_mode) |
274 (emu->address_mode ? MAP_PTI_MASK1 : MAP_PTI_MASK0);
275 snd_emu10k1_ptr_write_multiple(emu, voice,
276 // Not really necessary for the slave, but it doesn't hurt
277 CPF, stereo ? CPF_STEREO_MASK : 0,
278 // Assumption that PT is already 0 so no harm overwriting
279 PTRX, (send_amount[0] << 8) | send_amount[1],
280 // Stereo slaves don't need to have the addresses set, but it doesn't hurt
281 DSL, end_addr | (send_amount[3] << 24),
282 PSST, start_addr | (send_amount[2] << 24),
283 CCCA, emu10k1_select_interprom(evoice->epcm->pitch_target) |
284 (w_16 ? 0 : CCCA_8BITSELECT),
285 // Clear filter delay memory
286 Z1, 0,
287 Z2, 0,
288 // Invalidate maps
289 MAPA, silent_page,
290 MAPB, silent_page,
291 // Disable filter (in conjunction with CCCA_RESONANCE == 0)
292 VTFT, VTFT_FILTERTARGET_MASK,
293 CVCF, CVCF_CURRENTFILTER_MASK,
294 REGLIST_END);
295 // Setup routing
296 if (emu->audigy) {
297 snd_emu10k1_ptr_write_multiple(emu, voice,
298 A_FXRT1, snd_emu10k1_compose_audigy_fxrt1(send_routing),
299 A_FXRT2, snd_emu10k1_compose_audigy_fxrt2(send_routing),
300 A_SENDAMOUNTS, snd_emu10k1_compose_audigy_sendamounts(send_amount),
301 REGLIST_END);
302 for (int i = 0; i < 4; i++) {
303 u32 aml = emu10k1_send_target_from_amount(send_amount[2 * i]);
304 u32 amh = emu10k1_send_target_from_amount(send_amount[2 * i + 1]);
305 snd_emu10k1_ptr_write(emu, A_CSBA + i, voice, (amh << 16) | aml);
306 }
307 } else {
308 snd_emu10k1_ptr_write(emu, FXRT, voice,
309 snd_emu10k1_compose_send_routing(send_routing));
310 }
311
312 emu->voices[voice].dirty = 1;
313 }
314
snd_emu10k1_pcm_init_voices(struct snd_emu10k1 * emu,struct snd_emu10k1_voice * evoice,bool w_16,bool stereo,unsigned int start_addr,unsigned int end_addr,struct snd_emu10k1_pcm_mixer * mix)315 static void snd_emu10k1_pcm_init_voices(struct snd_emu10k1 *emu,
316 struct snd_emu10k1_voice *evoice,
317 bool w_16, bool stereo,
318 unsigned int start_addr,
319 unsigned int end_addr,
320 struct snd_emu10k1_pcm_mixer *mix)
321 {
322 guard(spinlock_irq)(&emu->reg_lock);
323 snd_emu10k1_pcm_init_voice(emu, evoice, w_16, stereo,
324 start_addr, end_addr,
325 &mix->send_routing[stereo][0],
326 &mix->send_volume[stereo][0]);
327 if (stereo)
328 snd_emu10k1_pcm_init_voice(emu, evoice + 1, w_16, true,
329 start_addr, end_addr,
330 &mix->send_routing[2][0],
331 &mix->send_volume[2][0]);
332 }
333
snd_emu10k1_pcm_init_extra_voice(struct snd_emu10k1 * emu,struct snd_emu10k1_voice * evoice,bool w_16,unsigned int start_addr,unsigned int end_addr)334 static void snd_emu10k1_pcm_init_extra_voice(struct snd_emu10k1 *emu,
335 struct snd_emu10k1_voice *evoice,
336 bool w_16,
337 unsigned int start_addr,
338 unsigned int end_addr)
339 {
340 static const unsigned char send_routing[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
341 static const unsigned char send_amount[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
342
343 snd_emu10k1_pcm_init_voice(emu, evoice, w_16, false,
344 start_addr, end_addr,
345 send_routing, send_amount);
346 }
347
snd_emu10k1_playback_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)348 static int snd_emu10k1_playback_hw_params(struct snd_pcm_substream *substream,
349 struct snd_pcm_hw_params *hw_params)
350 {
351 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
352 struct snd_pcm_runtime *runtime = substream->runtime;
353 struct snd_emu10k1_pcm *epcm = runtime->private_data;
354 size_t alloc_size;
355 int type, channels, count;
356 int err;
357
358 if (epcm->type == PLAYBACK_EMUVOICE) {
359 type = EMU10K1_PCM;
360 channels = 1;
361 count = params_channels(hw_params);
362 } else {
363 type = EMU10K1_EFX;
364 channels = params_channels(hw_params);
365 count = 1;
366 }
367 err = snd_emu10k1_pcm_channel_alloc(epcm, type, count, channels);
368 if (err < 0)
369 return err;
370
371 alloc_size = params_buffer_bytes(hw_params);
372 if (emu->iommu_workaround)
373 alloc_size += EMUPAGESIZE;
374 err = snd_pcm_lib_malloc_pages(substream, alloc_size);
375 if (err < 0)
376 return err;
377 if (emu->iommu_workaround && runtime->dma_bytes >= EMUPAGESIZE)
378 runtime->dma_bytes -= EMUPAGESIZE;
379 if (err > 0) { /* change */
380 int mapped;
381 if (epcm->memblk != NULL)
382 snd_emu10k1_free_pages(emu, epcm->memblk);
383 epcm->memblk = snd_emu10k1_alloc_pages(emu, substream);
384 epcm->start_addr = 0;
385 if (! epcm->memblk)
386 return -ENOMEM;
387 mapped = ((struct snd_emu10k1_memblk *)epcm->memblk)->mapped_page;
388 if (mapped < 0)
389 return -ENOMEM;
390 epcm->start_addr = mapped << PAGE_SHIFT;
391 }
392 return 0;
393 }
394
snd_emu10k1_playback_hw_free(struct snd_pcm_substream * substream)395 static int snd_emu10k1_playback_hw_free(struct snd_pcm_substream *substream)
396 {
397 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
398 struct snd_pcm_runtime *runtime = substream->runtime;
399 struct snd_emu10k1_pcm *epcm;
400
401 if (runtime->private_data == NULL)
402 return 0;
403 epcm = runtime->private_data;
404 if (epcm->extra) {
405 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
406 epcm->extra = NULL;
407 }
408 snd_emu10k1_pcm_free_voices(epcm);
409 if (epcm->memblk) {
410 snd_emu10k1_free_pages(emu, epcm->memblk);
411 epcm->memblk = NULL;
412 epcm->start_addr = 0;
413 }
414 snd_pcm_lib_free_pages(substream);
415 return 0;
416 }
417
snd_emu10k1_playback_prepare(struct snd_pcm_substream * substream)418 static int snd_emu10k1_playback_prepare(struct snd_pcm_substream *substream)
419 {
420 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
421 struct snd_pcm_runtime *runtime = substream->runtime;
422 struct snd_emu10k1_pcm *epcm = runtime->private_data;
423 bool w_16 = snd_pcm_format_width(runtime->format) == 16;
424 bool stereo = runtime->channels == 2;
425 unsigned int start_addr, end_addr;
426 unsigned int rate;
427
428 rate = runtime->rate;
429 if (emu->card_capabilities->emu_model &&
430 emu->emu1010.word_clock == 44100)
431 rate = rate * 480 / 441;
432 epcm->pitch_target = emu10k1_calc_pitch_target(rate);
433
434 start_addr = epcm->start_addr >> w_16;
435 end_addr = start_addr + runtime->period_size;
436 snd_emu10k1_pcm_init_extra_voice(emu, epcm->extra, w_16,
437 start_addr, end_addr);
438 start_addr >>= stereo;
439 epcm->ccca_start_addr = start_addr;
440 end_addr = start_addr + runtime->buffer_size;
441 snd_emu10k1_pcm_init_voices(emu, epcm->voices[0], w_16, stereo,
442 start_addr, end_addr,
443 &emu->pcm_mixer[substream->number]);
444
445 return 0;
446 }
447
snd_emu10k1_efx_playback_prepare(struct snd_pcm_substream * substream)448 static int snd_emu10k1_efx_playback_prepare(struct snd_pcm_substream *substream)
449 {
450 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
451 struct snd_pcm_runtime *runtime = substream->runtime;
452 struct snd_emu10k1_pcm *epcm = runtime->private_data;
453 unsigned int start_addr;
454 unsigned int extra_size, channel_size;
455 unsigned int i;
456
457 epcm->pitch_target = PITCH_48000;
458
459 start_addr = epcm->start_addr >> 1; // 16-bit voices
460
461 extra_size = runtime->period_size;
462 channel_size = runtime->buffer_size;
463
464 snd_emu10k1_pcm_init_extra_voice(emu, epcm->extra, true,
465 start_addr, start_addr + extra_size);
466
467 epcm->ccca_start_addr = start_addr;
468 for (i = 0; i < runtime->channels; i++) {
469 snd_emu10k1_pcm_init_voices(emu, epcm->voices[i], true, false,
470 start_addr, start_addr + channel_size,
471 &emu->efx_pcm_mixer[i]);
472 start_addr += channel_size;
473 }
474
475 return 0;
476 }
477
478 static const struct snd_pcm_hardware snd_emu10k1_efx_playback =
479 {
480 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_NONINTERLEAVED |
481 SNDRV_PCM_INFO_BLOCK_TRANSFER |
482 SNDRV_PCM_INFO_RESUME |
483 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
484 .formats = SNDRV_PCM_FMTBIT_S16_LE,
485 .rates = SNDRV_PCM_RATE_48000,
486 .rate_min = 48000,
487 .rate_max = 48000,
488 .channels_min = 1,
489 .channels_max = NUM_EFX_PLAYBACK,
490 .buffer_bytes_max = (128*1024),
491 .period_bytes_max = (128*1024),
492 .periods_min = 2,
493 .periods_max = 1024,
494 .fifo_size = 0,
495 };
496
snd_emu10k1_capture_prepare(struct snd_pcm_substream * substream)497 static int snd_emu10k1_capture_prepare(struct snd_pcm_substream *substream)
498 {
499 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
500 struct snd_pcm_runtime *runtime = substream->runtime;
501 struct snd_emu10k1_pcm *epcm = runtime->private_data;
502 int idx;
503
504 /* zeroing the buffer size will stop capture */
505 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
506 switch (epcm->type) {
507 case CAPTURE_AC97ADC:
508 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
509 break;
510 case CAPTURE_EFX:
511 if (emu->card_capabilities->emu_model) {
512 // The upper 32 16-bit capture voices, two for each of the 16 32-bit channels.
513 // The lower voices are occupied by A_EXTOUT_*_CAP*.
514 epcm->capture_cr_val = 0;
515 epcm->capture_cr_val2 = 0xffffffff >> (32 - runtime->channels * 2);
516 }
517 if (emu->audigy) {
518 snd_emu10k1_ptr_write_multiple(emu, 0,
519 A_FXWC1, 0,
520 A_FXWC2, 0,
521 REGLIST_END);
522 } else
523 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
524 break;
525 default:
526 break;
527 }
528 snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr);
529 epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream);
530 epcm->capture_bs_val = 0;
531 for (idx = 0; idx < 31; idx++) {
532 if (capture_buffer_sizes[idx] == epcm->capture_bufsize) {
533 epcm->capture_bs_val = idx + 1;
534 break;
535 }
536 }
537 if (epcm->capture_bs_val == 0) {
538 snd_BUG();
539 epcm->capture_bs_val++;
540 }
541 if (epcm->type == CAPTURE_AC97ADC) {
542 unsigned rate = runtime->rate;
543 if (!(runtime->hw.rates & SNDRV_PCM_RATE_48000))
544 rate = rate * 480 / 441;
545
546 epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE;
547 if (runtime->channels > 1)
548 epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE;
549 epcm->capture_cr_val |= emu->audigy ?
550 snd_emu10k1_audigy_capture_rate_reg(rate) :
551 snd_emu10k1_capture_rate_reg(rate);
552 }
553 return 0;
554 }
555
snd_emu10k1_playback_fill_cache(struct snd_emu10k1 * emu,unsigned voice,u32 sample,bool stereo)556 static void snd_emu10k1_playback_fill_cache(struct snd_emu10k1 *emu,
557 unsigned voice,
558 u32 sample, bool stereo)
559 {
560 u32 ccr;
561
562 // We assume that the cache is resting at this point (i.e.,
563 // CCR_CACHEINVALIDSIZE is very small).
564
565 // Clear leading frames. For simplicitly, this does too much,
566 // except for 16-bit stereo. And the interpolator will actually
567 // access them at all only when we're pitch-shifting.
568 for (int i = 0; i < 3; i++)
569 snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
570
571 // Fill cache
572 ccr = (64 - 3) << REG_SHIFT(CCR_CACHEINVALIDSIZE);
573 if (stereo) {
574 // The engine goes haywire if CCR_READADDRESS is out of sync
575 snd_emu10k1_ptr_write(emu, CCR, voice + 1, ccr);
576 }
577 snd_emu10k1_ptr_write(emu, CCR, voice, ccr);
578 }
579
snd_emu10k1_playback_prepare_voices(struct snd_emu10k1 * emu,struct snd_emu10k1_pcm * epcm,bool w_16,bool stereo,int channels)580 static void snd_emu10k1_playback_prepare_voices(struct snd_emu10k1 *emu,
581 struct snd_emu10k1_pcm *epcm,
582 bool w_16, bool stereo,
583 int channels)
584 {
585 struct snd_pcm_substream *substream = epcm->substream;
586 struct snd_pcm_runtime *runtime = substream->runtime;
587 unsigned eloop_start = epcm->start_addr >> w_16;
588 unsigned loop_start = eloop_start >> stereo;
589 unsigned eloop_size = runtime->period_size;
590 unsigned loop_size = runtime->buffer_size;
591 u32 sample = w_16 ? 0 : 0x80808080;
592
593 // To make the playback actually start at the 1st frame,
594 // we need to compensate for two circumstances:
595 // - The actual position is delayed by the cache size (64 frames)
596 // - The interpolator is centered around the 4th frame
597 loop_start += (epcm->resume_pos + 64 - 3) % loop_size;
598 for (int i = 0; i < channels; i++) {
599 unsigned voice = epcm->voices[i]->number;
600 snd_emu10k1_ptr_write(emu, CCCA_CURRADDR, voice, loop_start);
601 loop_start += loop_size;
602 snd_emu10k1_playback_fill_cache(emu, voice, sample, stereo);
603 }
604
605 // The interrupt is triggered when CCCA_CURRADDR (CA) wraps around,
606 // which is ahead of the actual playback position, so the interrupt
607 // source needs to be delayed.
608 //
609 // In principle, this wouldn't need to be the cache's entire size - in
610 // practice, CCR_CACHEINVALIDSIZE (CIS) > `fetch threshold` has never
611 // been observed, and assuming 40 _bytes_ should be safe.
612 //
613 // The cache fills are somewhat random, which makes it impossible to
614 // align them with the interrupts. This makes a non-delayed interrupt
615 // source not practical, as the interrupt handler would have to wait
616 // for (CA - CIS) >= period_boundary for every channel in the stream.
617 //
618 // This is why all other (open) drivers for these chips use timer-based
619 // interrupts.
620 //
621 eloop_start += (epcm->resume_pos + eloop_size - 3) % eloop_size;
622 snd_emu10k1_ptr_write(emu, CCCA_CURRADDR, epcm->extra->number, eloop_start);
623
624 // It takes a moment until the cache fills complete,
625 // but the unmuting takes long enough for that.
626 }
627
snd_emu10k1_playback_commit_volume(struct snd_emu10k1 * emu,struct snd_emu10k1_voice * evoice,unsigned int vattn)628 static void snd_emu10k1_playback_commit_volume(struct snd_emu10k1 *emu,
629 struct snd_emu10k1_voice *evoice,
630 unsigned int vattn)
631 {
632 snd_emu10k1_ptr_write_multiple(emu, evoice->number,
633 VTFT, vattn | VTFT_FILTERTARGET_MASK,
634 CVCF, vattn | CVCF_CURRENTFILTER_MASK,
635 REGLIST_END);
636 }
637
snd_emu10k1_playback_unmute_voice(struct snd_emu10k1 * emu,struct snd_emu10k1_voice * evoice,bool stereo,bool master,struct snd_emu10k1_pcm_mixer * mix)638 static void snd_emu10k1_playback_unmute_voice(struct snd_emu10k1 *emu,
639 struct snd_emu10k1_voice *evoice,
640 bool stereo, bool master,
641 struct snd_emu10k1_pcm_mixer *mix)
642 {
643 unsigned int vattn;
644 unsigned int tmp;
645
646 tmp = stereo ? (master ? 1 : 2) : 0;
647 vattn = mix->attn[tmp] << 16;
648 snd_emu10k1_playback_commit_volume(emu, evoice, vattn);
649 }
650
snd_emu10k1_playback_unmute_voices(struct snd_emu10k1 * emu,struct snd_emu10k1_voice * evoice,bool stereo,struct snd_emu10k1_pcm_mixer * mix)651 static void snd_emu10k1_playback_unmute_voices(struct snd_emu10k1 *emu,
652 struct snd_emu10k1_voice *evoice,
653 bool stereo,
654 struct snd_emu10k1_pcm_mixer *mix)
655 {
656 snd_emu10k1_playback_unmute_voice(emu, evoice, stereo, true, mix);
657 if (stereo)
658 snd_emu10k1_playback_unmute_voice(emu, evoice + 1, true, false, mix);
659 }
660
snd_emu10k1_playback_mute_voice(struct snd_emu10k1 * emu,struct snd_emu10k1_voice * evoice)661 static void snd_emu10k1_playback_mute_voice(struct snd_emu10k1 *emu,
662 struct snd_emu10k1_voice *evoice)
663 {
664 snd_emu10k1_playback_commit_volume(emu, evoice, 0);
665 }
666
snd_emu10k1_playback_mute_voices(struct snd_emu10k1 * emu,struct snd_emu10k1_voice * evoice,bool stereo)667 static void snd_emu10k1_playback_mute_voices(struct snd_emu10k1 *emu,
668 struct snd_emu10k1_voice *evoice,
669 bool stereo)
670 {
671 snd_emu10k1_playback_mute_voice(emu, evoice);
672 if (stereo)
673 snd_emu10k1_playback_mute_voice(emu, evoice + 1);
674 }
675
snd_emu10k1_playback_commit_pitch(struct snd_emu10k1 * emu,u32 voice,u32 pitch_target)676 static void snd_emu10k1_playback_commit_pitch(struct snd_emu10k1 *emu,
677 u32 voice, u32 pitch_target)
678 {
679 u32 ptrx = snd_emu10k1_ptr_read(emu, PTRX, voice);
680 u32 cpf = snd_emu10k1_ptr_read(emu, CPF, voice);
681 snd_emu10k1_ptr_write_multiple(emu, voice,
682 PTRX, (ptrx & ~PTRX_PITCHTARGET_MASK) | pitch_target,
683 CPF, (cpf & ~(CPF_CURRENTPITCH_MASK | CPF_FRACADDRESS_MASK)) | pitch_target,
684 REGLIST_END);
685 }
686
snd_emu10k1_playback_trigger_voice(struct snd_emu10k1 * emu,struct snd_emu10k1_voice * evoice)687 static void snd_emu10k1_playback_trigger_voice(struct snd_emu10k1 *emu,
688 struct snd_emu10k1_voice *evoice)
689 {
690 unsigned int voice;
691
692 voice = evoice->number;
693 snd_emu10k1_playback_commit_pitch(emu, voice, evoice->epcm->pitch_target << 16);
694 }
695
snd_emu10k1_playback_stop_voice(struct snd_emu10k1 * emu,struct snd_emu10k1_voice * evoice)696 static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu,
697 struct snd_emu10k1_voice *evoice)
698 {
699 unsigned int voice;
700
701 voice = evoice->number;
702 snd_emu10k1_playback_commit_pitch(emu, voice, 0);
703 }
704
snd_emu10k1_playback_set_running(struct snd_emu10k1 * emu,struct snd_emu10k1_pcm * epcm)705 static void snd_emu10k1_playback_set_running(struct snd_emu10k1 *emu,
706 struct snd_emu10k1_pcm *epcm)
707 {
708 epcm->running = 1;
709 snd_emu10k1_voice_intr_enable(emu, epcm->extra->number);
710 }
711
snd_emu10k1_playback_set_stopped(struct snd_emu10k1 * emu,struct snd_emu10k1_pcm * epcm)712 static void snd_emu10k1_playback_set_stopped(struct snd_emu10k1 *emu,
713 struct snd_emu10k1_pcm *epcm)
714 {
715 snd_emu10k1_voice_intr_disable(emu, epcm->extra->number);
716 epcm->running = 0;
717 }
718
snd_emu10k1_playback_trigger(struct snd_pcm_substream * substream,int cmd)719 static int snd_emu10k1_playback_trigger(struct snd_pcm_substream *substream,
720 int cmd)
721 {
722 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
723 struct snd_pcm_runtime *runtime = substream->runtime;
724 struct snd_emu10k1_pcm *epcm = runtime->private_data;
725 struct snd_emu10k1_pcm_mixer *mix;
726 bool w_16 = snd_pcm_format_width(runtime->format) == 16;
727 bool stereo = runtime->channels == 2;
728
729 /*
730 dev_dbg(emu->card->dev,
731 "trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n",
732 (int)emu, cmd, substream->ops->pointer(substream))
733 */
734 guard(spinlock)(&emu->reg_lock);
735 switch (cmd) {
736 case SNDRV_PCM_TRIGGER_START:
737 snd_emu10k1_playback_prepare_voices(emu, epcm, w_16, stereo, 1);
738 fallthrough;
739 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
740 case SNDRV_PCM_TRIGGER_RESUME:
741 mix = &emu->pcm_mixer[substream->number];
742 snd_emu10k1_playback_unmute_voices(emu, epcm->voices[0], stereo, mix);
743 snd_emu10k1_playback_set_running(emu, epcm);
744 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0]);
745 snd_emu10k1_playback_trigger_voice(emu, epcm->extra);
746 break;
747 case SNDRV_PCM_TRIGGER_STOP:
748 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
749 case SNDRV_PCM_TRIGGER_SUSPEND:
750 snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]);
751 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
752 snd_emu10k1_playback_set_stopped(emu, epcm);
753 snd_emu10k1_playback_mute_voices(emu, epcm->voices[0], stereo);
754 break;
755 default:
756 return -EINVAL;
757 }
758 return 0;
759 }
760
snd_emu10k1_capture_trigger(struct snd_pcm_substream * substream,int cmd)761 static int snd_emu10k1_capture_trigger(struct snd_pcm_substream *substream,
762 int cmd)
763 {
764 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
765 struct snd_pcm_runtime *runtime = substream->runtime;
766 struct snd_emu10k1_pcm *epcm = runtime->private_data;
767
768 guard(spinlock)(&emu->reg_lock);
769 switch (cmd) {
770 case SNDRV_PCM_TRIGGER_START:
771 case SNDRV_PCM_TRIGGER_RESUME:
772 /* hmm this should cause full and half full interrupt to be raised? */
773 outl(epcm->capture_ipr, emu->port + IPR);
774 snd_emu10k1_intr_enable(emu, epcm->capture_inte);
775 /*
776 dev_dbg(emu->card->dev, "adccr = 0x%x, adcbs = 0x%x\n",
777 epcm->adccr, epcm->adcbs);
778 */
779 switch (epcm->type) {
780 case CAPTURE_AC97ADC:
781 snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val);
782 break;
783 case CAPTURE_EFX:
784 if (emu->audigy) {
785 snd_emu10k1_ptr_write_multiple(emu, 0,
786 A_FXWC1, epcm->capture_cr_val,
787 A_FXWC2, epcm->capture_cr_val2,
788 REGLIST_END);
789 dev_dbg(emu->card->dev,
790 "cr_val=0x%x, cr_val2=0x%x\n",
791 epcm->capture_cr_val,
792 epcm->capture_cr_val2);
793 } else
794 snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val);
795 break;
796 default:
797 break;
798 }
799 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val);
800 epcm->running = 1;
801 epcm->first_ptr = 1;
802 break;
803 case SNDRV_PCM_TRIGGER_STOP:
804 case SNDRV_PCM_TRIGGER_SUSPEND:
805 epcm->running = 0;
806 snd_emu10k1_intr_disable(emu, epcm->capture_inte);
807 outl(epcm->capture_ipr, emu->port + IPR);
808 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
809 switch (epcm->type) {
810 case CAPTURE_AC97ADC:
811 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
812 break;
813 case CAPTURE_EFX:
814 if (emu->audigy) {
815 snd_emu10k1_ptr_write_multiple(emu, 0,
816 A_FXWC1, 0,
817 A_FXWC2, 0,
818 REGLIST_END);
819 } else
820 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
821 break;
822 default:
823 break;
824 }
825 break;
826 default:
827 return -EINVAL;
828 }
829 return 0;
830 }
831
snd_emu10k1_playback_pointer(struct snd_pcm_substream * substream)832 static snd_pcm_uframes_t snd_emu10k1_playback_pointer(struct snd_pcm_substream *substream)
833 {
834 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
835 struct snd_pcm_runtime *runtime = substream->runtime;
836 struct snd_emu10k1_pcm *epcm = runtime->private_data;
837 int ptr;
838
839 if (!epcm->running)
840 return 0;
841
842 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
843 ptr -= epcm->ccca_start_addr;
844
845 // This is the size of the whole cache minus the interpolator read-ahead,
846 // which leads us to the actual playback position.
847 //
848 // The cache is constantly kept mostly filled, so in principle we could
849 // return a more advanced position representing how far the hardware has
850 // already read the buffer, and set runtime->delay accordingly. However,
851 // this would be slightly different for every channel (and remarkably slow
852 // to obtain), so only a fixed worst-case value would be practical.
853 //
854 ptr -= 64 - 3;
855 if (ptr < 0)
856 ptr += runtime->buffer_size;
857
858 /*
859 dev_dbg(emu->card->dev,
860 "ptr = 0x%lx, buffer_size = 0x%lx, period_size = 0x%lx\n",
861 (long)ptr, (long)runtime->buffer_size,
862 (long)runtime->period_size);
863 */
864 return ptr;
865 }
866
snd_emu10k1_efx_playback_voice_mask(struct snd_emu10k1_pcm * epcm,int channels)867 static u64 snd_emu10k1_efx_playback_voice_mask(struct snd_emu10k1_pcm *epcm,
868 int channels)
869 {
870 u64 mask = 0;
871
872 for (int i = 0; i < channels; i++) {
873 int voice = epcm->voices[i]->number;
874 mask |= 1ULL << voice;
875 }
876 return mask;
877 }
878
snd_emu10k1_efx_playback_freeze_voices(struct snd_emu10k1 * emu,struct snd_emu10k1_pcm * epcm,int channels)879 static void snd_emu10k1_efx_playback_freeze_voices(struct snd_emu10k1 *emu,
880 struct snd_emu10k1_pcm *epcm,
881 int channels)
882 {
883 for (int i = 0; i < channels; i++) {
884 int voice = epcm->voices[i]->number;
885 snd_emu10k1_ptr_write(emu, CPF_STOP, voice, 1);
886 snd_emu10k1_playback_commit_pitch(emu, voice, PITCH_48000 << 16);
887 }
888 }
889
snd_emu10k1_efx_playback_unmute_voices(struct snd_emu10k1 * emu,struct snd_emu10k1_pcm * epcm,int channels)890 static void snd_emu10k1_efx_playback_unmute_voices(struct snd_emu10k1 *emu,
891 struct snd_emu10k1_pcm *epcm,
892 int channels)
893 {
894 for (int i = 0; i < channels; i++)
895 snd_emu10k1_playback_unmute_voice(emu, epcm->voices[i], false, true,
896 &emu->efx_pcm_mixer[i]);
897 }
898
snd_emu10k1_efx_playback_stop_voices(struct snd_emu10k1 * emu,struct snd_emu10k1_pcm * epcm,int channels)899 static void snd_emu10k1_efx_playback_stop_voices(struct snd_emu10k1 *emu,
900 struct snd_emu10k1_pcm *epcm,
901 int channels)
902 {
903 for (int i = 0; i < channels; i++)
904 snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
905 snd_emu10k1_playback_set_stopped(emu, epcm);
906
907 for (int i = 0; i < channels; i++)
908 snd_emu10k1_playback_mute_voice(emu, epcm->voices[i]);
909 }
910
snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream * substream,int cmd)911 static int snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream *substream,
912 int cmd)
913 {
914 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
915 struct snd_pcm_runtime *runtime = substream->runtime;
916 struct snd_emu10k1_pcm *epcm = runtime->private_data;
917 u64 mask;
918 int result = 0;
919
920 guard(spinlock)(&emu->reg_lock);
921 switch (cmd) {
922 case SNDRV_PCM_TRIGGER_START:
923 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
924 case SNDRV_PCM_TRIGGER_RESUME:
925 mask = snd_emu10k1_efx_playback_voice_mask(
926 epcm, runtime->channels);
927 for (int i = 0; i < 10; i++) {
928 // Note that the freeze is not interruptible, so we make no
929 // effort to reset the bits outside the error handling here.
930 snd_emu10k1_voice_set_loop_stop_multiple(emu, mask);
931 snd_emu10k1_efx_playback_freeze_voices(
932 emu, epcm, runtime->channels);
933 snd_emu10k1_playback_prepare_voices(
934 emu, epcm, true, false, runtime->channels);
935
936 // It might seem to make more sense to unmute the voices only after
937 // they have been started, to potentially avoid torturing the speakers
938 // if something goes wrong. However, we cannot unmute atomically,
939 // which means that we'd get some mild artifacts in the regular case.
940 snd_emu10k1_efx_playback_unmute_voices(emu, epcm, runtime->channels);
941
942 snd_emu10k1_playback_set_running(emu, epcm);
943 result = snd_emu10k1_voice_clear_loop_stop_multiple_atomic(emu, mask);
944 if (result == 0) {
945 // The extra voice is allowed to lag a bit
946 snd_emu10k1_playback_trigger_voice(emu, epcm->extra);
947 return 0;
948 }
949
950 snd_emu10k1_efx_playback_stop_voices(
951 emu, epcm, runtime->channels);
952
953 if (result != -EAGAIN)
954 break;
955 // The sync start can legitimately fail due to NMIs, etc.
956 }
957 snd_emu10k1_voice_clear_loop_stop_multiple(emu, mask);
958 break;
959 case SNDRV_PCM_TRIGGER_SUSPEND:
960 case SNDRV_PCM_TRIGGER_STOP:
961 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
962 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
963 snd_emu10k1_efx_playback_stop_voices(
964 emu, epcm, runtime->channels);
965
966 epcm->resume_pos = snd_emu10k1_playback_pointer(substream);
967 break;
968 default:
969 return -EINVAL;
970 }
971 return result;
972 }
973
974
snd_emu10k1_capture_pointer(struct snd_pcm_substream * substream)975 static snd_pcm_uframes_t snd_emu10k1_capture_pointer(struct snd_pcm_substream *substream)
976 {
977 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
978 struct snd_pcm_runtime *runtime = substream->runtime;
979 struct snd_emu10k1_pcm *epcm = runtime->private_data;
980 unsigned int ptr;
981
982 if (!epcm->running)
983 return 0;
984 if (epcm->first_ptr) {
985 udelay(50); /* hack, it takes awhile until capture is started */
986 epcm->first_ptr = 0;
987 }
988 ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff;
989 return bytes_to_frames(runtime, ptr);
990 }
991
992 /*
993 * Playback support device description
994 */
995
996 static const struct snd_pcm_hardware snd_emu10k1_playback =
997 {
998 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
999 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1000 SNDRV_PCM_INFO_RESUME |
1001 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
1002 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1003 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000,
1004 .rate_min = 4000,
1005 .rate_max = 96000,
1006 .channels_min = 1,
1007 .channels_max = 2,
1008 .buffer_bytes_max = (128*1024),
1009 .period_bytes_max = (128*1024),
1010 .periods_min = 2,
1011 .periods_max = 1024,
1012 .fifo_size = 0,
1013 };
1014
1015 /*
1016 * Capture support device description
1017 */
1018
1019 static const struct snd_pcm_hardware snd_emu10k1_capture =
1020 {
1021 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1022 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1023 SNDRV_PCM_INFO_RESUME |
1024 SNDRV_PCM_INFO_MMAP_VALID),
1025 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1026 .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_24000,
1027 .rate_min = 8000,
1028 .rate_max = 48000,
1029 .channels_min = 1,
1030 .channels_max = 2,
1031 .buffer_bytes_max = (64*1024),
1032 .period_bytes_min = 384,
1033 .period_bytes_max = (64*1024),
1034 .periods_min = 2,
1035 .periods_max = 2,
1036 .fifo_size = 0,
1037 };
1038
1039 static const struct snd_pcm_hardware snd_emu10k1_capture_efx =
1040 {
1041 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1042 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1043 SNDRV_PCM_INFO_RESUME |
1044 SNDRV_PCM_INFO_MMAP_VALID),
1045 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1046 .rates = SNDRV_PCM_RATE_48000,
1047 .rate_min = 48000,
1048 .rate_max = 48000,
1049 .channels_min = 1,
1050 .channels_max = 16,
1051 .buffer_bytes_max = (64*1024),
1052 .period_bytes_min = 384,
1053 .period_bytes_max = (64*1024),
1054 .periods_min = 2,
1055 .periods_max = 2,
1056 .fifo_size = 0,
1057 };
1058
1059 /*
1060 *
1061 */
1062
snd_emu10k1_pcm_mixer_notify1(struct snd_emu10k1 * emu,struct snd_kcontrol * kctl,int idx,int activate)1063 static void snd_emu10k1_pcm_mixer_notify1(struct snd_emu10k1 *emu, struct snd_kcontrol *kctl, int idx, int activate)
1064 {
1065 struct snd_ctl_elem_id id;
1066
1067 if (! kctl)
1068 return;
1069 if (activate)
1070 kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1071 else
1072 kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1073 snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE |
1074 SNDRV_CTL_EVENT_MASK_INFO,
1075 snd_ctl_build_ioff(&id, kctl, idx));
1076 }
1077
snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 * emu,int idx,int activate)1078 static void snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1079 {
1080 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate);
1081 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate);
1082 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate);
1083 }
1084
snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 * emu,int idx,int activate)1085 static void snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1086 {
1087 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate);
1088 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate);
1089 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate);
1090 }
1091
snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime * runtime)1092 static void snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime *runtime)
1093 {
1094 kfree(runtime->private_data);
1095 }
1096
snd_emu10k1_efx_playback_close(struct snd_pcm_substream * substream)1097 static int snd_emu10k1_efx_playback_close(struct snd_pcm_substream *substream)
1098 {
1099 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1100 struct snd_emu10k1_pcm_mixer *mix;
1101 int i;
1102
1103 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1104 mix = &emu->efx_pcm_mixer[i];
1105 mix->epcm = NULL;
1106 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
1107 }
1108 return 0;
1109 }
1110
snd_emu10k1_playback_set_constraints(struct snd_pcm_runtime * runtime)1111 static int snd_emu10k1_playback_set_constraints(struct snd_pcm_runtime *runtime)
1112 {
1113 int err;
1114
1115 // The buffer size must be a multiple of the period size, to avoid a
1116 // mismatch between the extra voice and the regular voices.
1117 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
1118 if (err < 0)
1119 return err;
1120 // The hardware is typically the cache's size of 64 frames ahead.
1121 // Leave enough time for actually filling up the buffer.
1122 err = snd_pcm_hw_constraint_minmax(
1123 runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 128, UINT_MAX);
1124 return err;
1125 }
1126
snd_emu10k1_efx_playback_open(struct snd_pcm_substream * substream)1127 static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream)
1128 {
1129 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1130 struct snd_emu10k1_pcm *epcm;
1131 struct snd_emu10k1_pcm_mixer *mix;
1132 struct snd_pcm_runtime *runtime = substream->runtime;
1133 int i, j, err;
1134
1135 epcm = kzalloc_obj(*epcm);
1136 if (epcm == NULL)
1137 return -ENOMEM;
1138 epcm->emu = emu;
1139 epcm->type = PLAYBACK_EFX;
1140 epcm->substream = substream;
1141
1142 runtime->private_data = epcm;
1143 runtime->private_free = snd_emu10k1_pcm_free_substream;
1144 runtime->hw = snd_emu10k1_efx_playback;
1145 if (emu->card_capabilities->emu_model)
1146 snd_emu1010_constrain_efx_rate(emu, runtime);
1147 err = snd_emu10k1_playback_set_constraints(runtime);
1148 if (err < 0) {
1149 kfree(epcm);
1150 return err;
1151 }
1152
1153 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1154 mix = &emu->efx_pcm_mixer[i];
1155 for (j = 0; j < 8; j++)
1156 mix->send_routing[0][j] = i + j;
1157 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1158 mix->send_volume[0][0] = 255;
1159 mix->attn[0] = 0x8000;
1160 mix->epcm = epcm;
1161 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
1162 }
1163 return 0;
1164 }
1165
snd_emu10k1_playback_open(struct snd_pcm_substream * substream)1166 static int snd_emu10k1_playback_open(struct snd_pcm_substream *substream)
1167 {
1168 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1169 struct snd_emu10k1_pcm *epcm;
1170 struct snd_emu10k1_pcm_mixer *mix;
1171 struct snd_pcm_runtime *runtime = substream->runtime;
1172 int i, err, sample_rate;
1173
1174 epcm = kzalloc_obj(*epcm);
1175 if (epcm == NULL)
1176 return -ENOMEM;
1177 epcm->emu = emu;
1178 epcm->type = PLAYBACK_EMUVOICE;
1179 epcm->substream = substream;
1180 runtime->private_data = epcm;
1181 runtime->private_free = snd_emu10k1_pcm_free_substream;
1182 runtime->hw = snd_emu10k1_playback;
1183 err = snd_emu10k1_playback_set_constraints(runtime);
1184 if (err < 0) {
1185 kfree(epcm);
1186 return err;
1187 }
1188 if (emu->card_capabilities->emu_model)
1189 sample_rate = emu->emu1010.word_clock;
1190 else
1191 sample_rate = 48000;
1192 err = snd_pcm_hw_rule_noresample(runtime, sample_rate);
1193 if (err < 0) {
1194 kfree(epcm);
1195 return err;
1196 }
1197 mix = &emu->pcm_mixer[substream->number];
1198 for (i = 0; i < 8; i++)
1199 mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
1200 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1201 mix->send_volume[0][0] = mix->send_volume[0][1] =
1202 mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
1203 mix->attn[0] = mix->attn[1] = mix->attn[2] = 0x8000;
1204 mix->epcm = epcm;
1205 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
1206 return 0;
1207 }
1208
snd_emu10k1_playback_close(struct snd_pcm_substream * substream)1209 static int snd_emu10k1_playback_close(struct snd_pcm_substream *substream)
1210 {
1211 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1212 struct snd_emu10k1_pcm_mixer *mix = &emu->pcm_mixer[substream->number];
1213
1214 mix->epcm = NULL;
1215 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
1216 return 0;
1217 }
1218
snd_emu10k1_capture_open(struct snd_pcm_substream * substream)1219 static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream)
1220 {
1221 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1222 struct snd_pcm_runtime *runtime = substream->runtime;
1223 struct snd_emu10k1_pcm *epcm;
1224
1225 epcm = kzalloc_obj(*epcm);
1226 if (epcm == NULL)
1227 return -ENOMEM;
1228 epcm->emu = emu;
1229 epcm->type = CAPTURE_AC97ADC;
1230 epcm->substream = substream;
1231 epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
1232 epcm->capture_inte = INTE_ADCBUFENABLE;
1233 epcm->capture_ba_reg = ADCBA;
1234 epcm->capture_bs_reg = ADCBS;
1235 epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
1236 runtime->private_data = epcm;
1237 runtime->private_free = snd_emu10k1_pcm_free_substream;
1238 runtime->hw = snd_emu10k1_capture;
1239 snd_emu10k1_constrain_capture_rates(emu, runtime);
1240 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1241 &hw_constraints_capture_buffer_sizes);
1242 emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
1243 emu->pcm_capture_substream = substream;
1244 return 0;
1245 }
1246
snd_emu10k1_capture_close(struct snd_pcm_substream * substream)1247 static int snd_emu10k1_capture_close(struct snd_pcm_substream *substream)
1248 {
1249 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1250
1251 emu->capture_interrupt = NULL;
1252 emu->pcm_capture_substream = NULL;
1253 return 0;
1254 }
1255
snd_emu10k1_capture_mic_open(struct snd_pcm_substream * substream)1256 static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream)
1257 {
1258 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1259 struct snd_emu10k1_pcm *epcm;
1260 struct snd_pcm_runtime *runtime = substream->runtime;
1261
1262 epcm = kzalloc_obj(*epcm);
1263 if (epcm == NULL)
1264 return -ENOMEM;
1265 epcm->emu = emu;
1266 epcm->type = CAPTURE_AC97MIC;
1267 epcm->substream = substream;
1268 epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
1269 epcm->capture_inte = INTE_MICBUFENABLE;
1270 epcm->capture_ba_reg = MICBA;
1271 epcm->capture_bs_reg = MICBS;
1272 epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
1273 substream->runtime->private_data = epcm;
1274 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1275 runtime->hw = snd_emu10k1_capture;
1276 runtime->hw.rates = SNDRV_PCM_RATE_8000;
1277 runtime->hw.rate_min = runtime->hw.rate_max = 8000;
1278 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1279 &hw_constraints_capture_buffer_sizes);
1280 emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
1281 emu->pcm_capture_mic_substream = substream;
1282 return 0;
1283 }
1284
snd_emu10k1_capture_mic_close(struct snd_pcm_substream * substream)1285 static int snd_emu10k1_capture_mic_close(struct snd_pcm_substream *substream)
1286 {
1287 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1288
1289 emu->capture_mic_interrupt = NULL;
1290 emu->pcm_capture_mic_substream = NULL;
1291 return 0;
1292 }
1293
snd_emu10k1_capture_efx_open(struct snd_pcm_substream * substream)1294 static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream)
1295 {
1296 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1297 struct snd_emu10k1_pcm *epcm;
1298 struct snd_pcm_runtime *runtime = substream->runtime;
1299 int nefx = emu->audigy ? 64 : 32;
1300 int idx, err;
1301
1302 epcm = kzalloc_obj(*epcm);
1303 if (epcm == NULL)
1304 return -ENOMEM;
1305 epcm->emu = emu;
1306 epcm->type = CAPTURE_EFX;
1307 epcm->substream = substream;
1308 epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
1309 epcm->capture_inte = INTE_EFXBUFENABLE;
1310 epcm->capture_ba_reg = FXBA;
1311 epcm->capture_bs_reg = FXBS;
1312 epcm->capture_idx_reg = FXIDX;
1313 substream->runtime->private_data = epcm;
1314 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1315 runtime->hw = snd_emu10k1_capture_efx;
1316 if (emu->card_capabilities->emu_model) {
1317 snd_emu1010_constrain_efx_rate(emu, runtime);
1318 /*
1319 * There are 32 mono channels of 16bits each.
1320 * 24bit Audio uses 2x channels over 16bit,
1321 * 96kHz uses 2x channels over 48kHz,
1322 * 192kHz uses 4x channels over 48kHz.
1323 * So, for 48kHz 24bit, one has 16 channels,
1324 * for 96kHz 24bit, one has 8 channels,
1325 * for 192kHz 24bit, one has 4 channels.
1326 * 1010rev2 and 1616(m) cards have double that,
1327 * but we don't exceed 16 channels anyway.
1328 */
1329 #if 0
1330 /* For 96kHz */
1331 runtime->hw.channels_min = runtime->hw.channels_max = 4;
1332 #endif
1333 #if 0
1334 /* For 192kHz */
1335 runtime->hw.channels_min = runtime->hw.channels_max = 2;
1336 #endif
1337 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
1338 } else {
1339 guard(spinlock_irq)(&emu->reg_lock);
1340 runtime->hw.channels_min = runtime->hw.channels_max = 0;
1341 for (idx = 0; idx < nefx; idx++) {
1342 if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
1343 runtime->hw.channels_min++;
1344 runtime->hw.channels_max++;
1345 }
1346 }
1347 epcm->capture_cr_val = emu->efx_voices_mask[0];
1348 epcm->capture_cr_val2 = emu->efx_voices_mask[1];
1349 }
1350 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1351 &hw_constraints_efx_capture_channels);
1352 if (err < 0) {
1353 kfree(epcm);
1354 return err;
1355 }
1356 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1357 &hw_constraints_capture_buffer_sizes);
1358 emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
1359 emu->pcm_capture_efx_substream = substream;
1360 return 0;
1361 }
1362
snd_emu10k1_capture_efx_close(struct snd_pcm_substream * substream)1363 static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream)
1364 {
1365 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1366
1367 emu->capture_efx_interrupt = NULL;
1368 emu->pcm_capture_efx_substream = NULL;
1369 return 0;
1370 }
1371
1372 static const struct snd_pcm_ops snd_emu10k1_playback_ops = {
1373 .open = snd_emu10k1_playback_open,
1374 .close = snd_emu10k1_playback_close,
1375 .hw_params = snd_emu10k1_playback_hw_params,
1376 .hw_free = snd_emu10k1_playback_hw_free,
1377 .prepare = snd_emu10k1_playback_prepare,
1378 .trigger = snd_emu10k1_playback_trigger,
1379 .pointer = snd_emu10k1_playback_pointer,
1380 };
1381
1382 static const struct snd_pcm_ops snd_emu10k1_capture_ops = {
1383 .open = snd_emu10k1_capture_open,
1384 .close = snd_emu10k1_capture_close,
1385 .prepare = snd_emu10k1_capture_prepare,
1386 .trigger = snd_emu10k1_capture_trigger,
1387 .pointer = snd_emu10k1_capture_pointer,
1388 };
1389
1390 /* EFX playback */
1391 static const struct snd_pcm_ops snd_emu10k1_efx_playback_ops = {
1392 .open = snd_emu10k1_efx_playback_open,
1393 .close = snd_emu10k1_efx_playback_close,
1394 .hw_params = snd_emu10k1_playback_hw_params,
1395 .hw_free = snd_emu10k1_playback_hw_free,
1396 .prepare = snd_emu10k1_efx_playback_prepare,
1397 .trigger = snd_emu10k1_efx_playback_trigger,
1398 .pointer = snd_emu10k1_playback_pointer,
1399 };
1400
snd_emu10k1_pcm(struct snd_emu10k1 * emu,int device)1401 int snd_emu10k1_pcm(struct snd_emu10k1 *emu, int device)
1402 {
1403 struct snd_pcm *pcm;
1404 struct snd_pcm_substream *substream;
1405 int err;
1406
1407 err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm);
1408 if (err < 0)
1409 return err;
1410
1411 pcm->private_data = emu;
1412
1413 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
1414 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
1415
1416 pcm->info_flags = 0;
1417 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1418 strscpy(pcm->name, "ADC Capture/Standard PCM Playback");
1419 emu->pcm = pcm;
1420
1421 /* playback substream can't use managed buffers due to alignment */
1422 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1423 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
1424 &emu->pci->dev,
1425 64*1024, 64*1024);
1426
1427 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
1428 snd_pcm_set_managed_buffer(substream, SNDRV_DMA_TYPE_DEV,
1429 &emu->pci->dev, 64*1024, 64*1024);
1430
1431 return 0;
1432 }
1433
snd_emu10k1_pcm_multi(struct snd_emu10k1 * emu,int device)1434 int snd_emu10k1_pcm_multi(struct snd_emu10k1 *emu, int device)
1435 {
1436 struct snd_pcm *pcm;
1437 struct snd_pcm_substream *substream;
1438 int err;
1439
1440 err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm);
1441 if (err < 0)
1442 return err;
1443
1444 pcm->private_data = emu;
1445
1446 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);
1447
1448 pcm->info_flags = 0;
1449 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1450 strscpy(pcm->name, "Multichannel Playback");
1451 emu->pcm_multi = pcm;
1452
1453 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1454 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
1455 &emu->pci->dev,
1456 64*1024, 64*1024);
1457
1458 return 0;
1459 }
1460
1461
1462 static const struct snd_pcm_ops snd_emu10k1_capture_mic_ops = {
1463 .open = snd_emu10k1_capture_mic_open,
1464 .close = snd_emu10k1_capture_mic_close,
1465 .prepare = snd_emu10k1_capture_prepare,
1466 .trigger = snd_emu10k1_capture_trigger,
1467 .pointer = snd_emu10k1_capture_pointer,
1468 };
1469
snd_emu10k1_pcm_mic(struct snd_emu10k1 * emu,int device)1470 int snd_emu10k1_pcm_mic(struct snd_emu10k1 *emu, int device)
1471 {
1472 struct snd_pcm *pcm;
1473 int err;
1474
1475 err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm);
1476 if (err < 0)
1477 return err;
1478
1479 pcm->private_data = emu;
1480
1481 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
1482
1483 pcm->info_flags = 0;
1484 strscpy(pcm->name, "Mic Capture");
1485 emu->pcm_mic = pcm;
1486
1487 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &emu->pci->dev,
1488 64*1024, 64*1024);
1489
1490 return 0;
1491 }
1492
snd_emu10k1_pcm_efx_voices_mask_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1493 static int snd_emu10k1_pcm_efx_voices_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1494 {
1495 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1496 int nefx = emu->audigy ? 64 : 32;
1497 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1498 uinfo->count = nefx;
1499 uinfo->value.integer.min = 0;
1500 uinfo->value.integer.max = 1;
1501 return 0;
1502 }
1503
snd_emu10k1_pcm_efx_voices_mask_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1504 static int snd_emu10k1_pcm_efx_voices_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1505 {
1506 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1507 int nefx = emu->audigy ? 64 : 32;
1508 int idx;
1509
1510 for (idx = 0; idx < nefx; idx++)
1511 ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
1512 return 0;
1513 }
1514
snd_emu10k1_pcm_efx_voices_mask_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1515 static int snd_emu10k1_pcm_efx_voices_mask_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1516 {
1517 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1518 unsigned int nval[2], bits;
1519 int nefx = emu->audigy ? 64 : 32;
1520 int change, idx;
1521
1522 nval[0] = nval[1] = 0;
1523 for (idx = 0, bits = 0; idx < nefx; idx++)
1524 if (ucontrol->value.integer.value[idx]) {
1525 nval[idx / 32] |= 1 << (idx % 32);
1526 bits++;
1527 }
1528
1529 if (bits == 9 || bits == 11 || bits == 13 || bits == 15 || bits > 16)
1530 return -EINVAL;
1531
1532 guard(spinlock_irq)(&emu->reg_lock);
1533 change = (nval[0] != emu->efx_voices_mask[0]) ||
1534 (nval[1] != emu->efx_voices_mask[1]);
1535 emu->efx_voices_mask[0] = nval[0];
1536 emu->efx_voices_mask[1] = nval[1];
1537 return change;
1538 }
1539
1540 static const struct snd_kcontrol_new snd_emu10k1_pcm_efx_voices_mask = {
1541 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1542 .name = "Captured FX8010 Outputs",
1543 .info = snd_emu10k1_pcm_efx_voices_mask_info,
1544 .get = snd_emu10k1_pcm_efx_voices_mask_get,
1545 .put = snd_emu10k1_pcm_efx_voices_mask_put
1546 };
1547
1548 static const struct snd_pcm_ops snd_emu10k1_capture_efx_ops = {
1549 .open = snd_emu10k1_capture_efx_open,
1550 .close = snd_emu10k1_capture_efx_close,
1551 .prepare = snd_emu10k1_capture_prepare,
1552 .trigger = snd_emu10k1_capture_trigger,
1553 .pointer = snd_emu10k1_capture_pointer,
1554 };
1555
1556
1557 /* EFX playback */
1558
1559 #define INITIAL_TRAM_SHIFT 14
1560 #define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
1561
snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 * emu,void * private_data)1562 static void snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 *emu, void *private_data)
1563 {
1564 struct snd_pcm_substream *substream = private_data;
1565 snd_pcm_period_elapsed(substream);
1566 }
1567
snd_emu10k1_fx8010_playback_tram_poke1(unsigned short * dst_left,unsigned short * dst_right,unsigned short * src,unsigned int count,unsigned int tram_shift)1568 static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
1569 unsigned short *dst_right,
1570 unsigned short *src,
1571 unsigned int count,
1572 unsigned int tram_shift)
1573 {
1574 /*
1575 dev_dbg(emu->card->dev,
1576 "tram_poke1: dst_left = 0x%p, dst_right = 0x%p, "
1577 "src = 0x%p, count = 0x%x\n",
1578 dst_left, dst_right, src, count);
1579 */
1580 if ((tram_shift & 1) == 0) {
1581 while (count--) {
1582 *dst_left-- = *src++;
1583 *dst_right-- = *src++;
1584 }
1585 } else {
1586 while (count--) {
1587 *dst_right-- = *src++;
1588 *dst_left-- = *src++;
1589 }
1590 }
1591 }
1592
fx8010_pb_trans_copy(struct snd_pcm_substream * substream,struct snd_pcm_indirect * rec,size_t bytes)1593 static void fx8010_pb_trans_copy(struct snd_pcm_substream *substream,
1594 struct snd_pcm_indirect *rec, size_t bytes)
1595 {
1596 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1597 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1598 unsigned int tram_size = pcm->buffer_size;
1599 unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data);
1600 unsigned int frames = bytes >> 2, count;
1601 unsigned int tram_pos = pcm->tram_pos;
1602 unsigned int tram_shift = pcm->tram_shift;
1603
1604 while (frames > tram_pos) {
1605 count = tram_pos + 1;
1606 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1607 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1608 src, count, tram_shift);
1609 src += count * 2;
1610 frames -= count;
1611 tram_pos = (tram_size / 2) - 1;
1612 tram_shift++;
1613 }
1614 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1615 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1616 src, frames, tram_shift);
1617 tram_pos -= frames;
1618 pcm->tram_pos = tram_pos;
1619 pcm->tram_shift = tram_shift;
1620 }
1621
snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream * substream)1622 static int snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream *substream)
1623 {
1624 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1625 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1626
1627 return snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec,
1628 fx8010_pb_trans_copy);
1629 }
1630
snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream * substream)1631 static int snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream *substream)
1632 {
1633 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1634 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1635 unsigned int i;
1636
1637 for (i = 0; i < pcm->channels; i++)
1638 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0);
1639 return 0;
1640 }
1641
snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream * substream)1642 static int snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream *substream)
1643 {
1644 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1645 struct snd_pcm_runtime *runtime = substream->runtime;
1646 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1647 unsigned int i;
1648
1649 /*
1650 dev_dbg(emu->card->dev, "prepare: etram_pages = 0x%p, dma_area = 0x%x, "
1651 "buffer_size = 0x%x (0x%x)\n",
1652 emu->fx8010.etram_pages, runtime->dma_area,
1653 runtime->buffer_size, runtime->buffer_size << 2);
1654 */
1655 memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec));
1656 pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */
1657 pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1658 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1659 pcm->tram_shift = 0;
1660 snd_emu10k1_ptr_write_multiple(emu, 0,
1661 emu->gpr_base + pcm->gpr_running, 0, /* reset */
1662 emu->gpr_base + pcm->gpr_trigger, 0, /* reset */
1663 emu->gpr_base + pcm->gpr_size, runtime->buffer_size,
1664 emu->gpr_base + pcm->gpr_ptr, 0, /* reset ptr number */
1665 emu->gpr_base + pcm->gpr_count, runtime->period_size,
1666 emu->gpr_base + pcm->gpr_tmpcount, runtime->period_size,
1667 REGLIST_END);
1668 for (i = 0; i < pcm->channels; i++)
1669 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
1670 return 0;
1671 }
1672
snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream * substream,int cmd)1673 static int snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream *substream, int cmd)
1674 {
1675 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1676 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1677 int result;
1678
1679 guard(spinlock)(&emu->reg_lock);
1680 switch (cmd) {
1681 case SNDRV_PCM_TRIGGER_START:
1682 /* follow thru */
1683 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1684 case SNDRV_PCM_TRIGGER_RESUME:
1685 #ifdef EMU10K1_SET_AC3_IEC958
1686 {
1687 int i;
1688 for (i = 0; i < 3; i++) {
1689 unsigned int bits;
1690 bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1691 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
1692 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA;
1693 snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits);
1694 }
1695 }
1696 #endif
1697 result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq);
1698 if (result < 0)
1699 return result;
1700 snd_emu10k1_fx8010_playback_transfer(substream); /* roll the ball */
1701 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1);
1702 break;
1703 case SNDRV_PCM_TRIGGER_STOP:
1704 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1705 case SNDRV_PCM_TRIGGER_SUSPEND:
1706 snd_emu10k1_fx8010_unregister_irq_handler(emu, &pcm->irq);
1707 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);
1708 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1709 pcm->tram_shift = 0;
1710 break;
1711 default:
1712 return -EINVAL;
1713 }
1714 return 0;
1715 }
1716
snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream * substream)1717 static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream *substream)
1718 {
1719 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1720 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1721 size_t ptr; /* byte pointer */
1722
1723 if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0))
1724 return 0;
1725 ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2;
1726 return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr);
1727 }
1728
1729 static const struct snd_pcm_hardware snd_emu10k1_fx8010_playback =
1730 {
1731 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1732 SNDRV_PCM_INFO_RESUME |
1733 /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE |
1734 SNDRV_PCM_INFO_SYNC_APPLPTR),
1735 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1736 .rates = SNDRV_PCM_RATE_48000,
1737 .rate_min = 48000,
1738 .rate_max = 48000,
1739 .channels_min = 1,
1740 .channels_max = 1,
1741 .buffer_bytes_max = (128*1024),
1742 .period_bytes_min = 1024,
1743 .period_bytes_max = (128*1024),
1744 .periods_min = 2,
1745 .periods_max = 1024,
1746 .fifo_size = 0,
1747 };
1748
snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream * substream)1749 static int snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream *substream)
1750 {
1751 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1752 struct snd_pcm_runtime *runtime = substream->runtime;
1753 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1754
1755 runtime->hw = snd_emu10k1_fx8010_playback;
1756 runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels;
1757 runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2;
1758 guard(spinlock_irq)(&emu->reg_lock);
1759 if (pcm->valid == 0)
1760 return -ENODEV;
1761 pcm->opened = 1;
1762 return 0;
1763 }
1764
snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream * substream)1765 static int snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream *substream)
1766 {
1767 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1768 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1769
1770 guard(spinlock_irq)(&emu->reg_lock);
1771 pcm->opened = 0;
1772 return 0;
1773 }
1774
1775 static const struct snd_pcm_ops snd_emu10k1_fx8010_playback_ops = {
1776 .open = snd_emu10k1_fx8010_playback_open,
1777 .close = snd_emu10k1_fx8010_playback_close,
1778 .hw_free = snd_emu10k1_fx8010_playback_hw_free,
1779 .prepare = snd_emu10k1_fx8010_playback_prepare,
1780 .trigger = snd_emu10k1_fx8010_playback_trigger,
1781 .pointer = snd_emu10k1_fx8010_playback_pointer,
1782 .ack = snd_emu10k1_fx8010_playback_transfer,
1783 };
1784
snd_emu10k1_pcm_efx(struct snd_emu10k1 * emu,int device)1785 int snd_emu10k1_pcm_efx(struct snd_emu10k1 *emu, int device)
1786 {
1787 struct snd_pcm *pcm;
1788 struct snd_kcontrol *kctl;
1789 int err;
1790
1791 err = snd_pcm_new(emu->card, "emu10k1 efx", device, emu->audigy ? 0 : 8, 1, &pcm);
1792 if (err < 0)
1793 return err;
1794
1795 pcm->private_data = emu;
1796
1797 if (!emu->audigy)
1798 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
1799 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
1800
1801 pcm->info_flags = 0;
1802 if (emu->audigy)
1803 strscpy(pcm->name, "Multichannel Capture");
1804 else
1805 strscpy(pcm->name, "Multichannel Capture/PT Playback");
1806 emu->pcm_efx = pcm;
1807
1808 if (!emu->card_capabilities->emu_model) {
1809 // On Sound Blasters, the DSP code copies the EXTINs to FXBUS2.
1810 // The mask determines which of these and the EXTOUTs the multi-
1811 // channel capture actually records (the channel order is fixed).
1812 if (emu->audigy) {
1813 emu->efx_voices_mask[0] = 0;
1814 emu->efx_voices_mask[1] = 0xffff;
1815 } else {
1816 emu->efx_voices_mask[0] = 0xffff0000;
1817 emu->efx_voices_mask[1] = 0;
1818 }
1819 kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
1820 if (!kctl)
1821 return -ENOMEM;
1822 kctl->id.device = device;
1823 err = snd_ctl_add(emu->card, kctl);
1824 if (err < 0)
1825 return err;
1826 } else {
1827 // On E-MU cards, the DSP code copies the P16VINs/EMU32INs to
1828 // FXBUS2. These are already selected & routed by the FPGA,
1829 // so there is no need to apply additional masking.
1830 }
1831
1832 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &emu->pci->dev,
1833 64*1024, 64*1024);
1834
1835 return 0;
1836 }
1837